ScriptEngine returns 5 when asked questions the doubles - double

I use the code below to make my scriptEngine. It worked fine until I tried to do calculations containing doubles. The code below should return 30 but instead returns 5. Am I doing something wrong or can the scriptEngine just not do doubles?
String calculation = "20*1.5";
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String s = engine.eval(calculation).toString();
System.out.println("Test: " + s);
double answer = Double.parseDouble(s);
System.out.println("Answer: \t" + answer);

Related

Nnet in caret, basic structure

I'm very new to caret package and nnet in R. I've done some projects related to ANN with Matlab before, but now I need to work with R and I need some basic help.
My input dataset has 1000 observations (in rows) and 23 variables (in columns). My output has 1000 observations and 12 variables.
Here are some sample data that represent my dataset and might help to understand my problem better:
input = as.data.frame(matrix(sample(1 : 20, 100, replace = TRUE), ncol = 10))
colnames(input) = paste ( "X" , 1:10, sep = "") #10 observations and 10 variables
output = as.data.frame(matrix(sample(1 : 20, 70, replace = TRUE), ncol = 7))
colnames(output) = paste ( "Y" , 1:7, sep = "") #10 observations and 7 variables
#nnet with caret:
net1 = train(output ~., data = input, method= "nnet", maxit = 1000)
When I run the code, I get this error:
error: invalid type (list) for variable 'output'.
I think I have to add all output variables separately (which is very annoying, especially with a lot of variables), like this:
train(output$Y1 + output$Y2 + output$Y3 + output$Y4 + output$Y5 +
output$Y6 + output$Y7 ~., data = input, method= "nnet", maxit = 1000)
This time it runs but I get this error:
Error in [.data.frame(data, , all.vars(Terms), drop = FALSE) :
undefined columns selected
I try to use neuralnet package, with the code below it works perfectly but I still have to add output variables separately :(
net1 = neuralnet(output$Y1 + output$Y2 + output$Y3 + output$Y4 +
output$Y5 + output$Y6 + output$Y7 ~., data = input, hidden=c(2,10))
p.s. since these sample data are created randomly, the neuralnet cannot converge, but in my real data it works well (in comparison to Matlab ANN)
Now, if you could help me with a way to put output variables automatically (not manually), it solves my problem (although with neuralnet not caret).
use the str() function and ascertain that its a data frame looks like you are inputting a list to the train function. This may be because of a transformation you are doing before to output.
str(output)
Without a full script of earlier steps its difficult to understand what is going on.
After trying different things and searches, I finally found a solution:
First, we must use as.formula to show the relation between our input and output. With the code below we don't need to add all the variables separately:
names1 <- colnames(output) #the name of our variables in the output
names2 = colnames(input) #the name of our variables in the input
a <- as.formula(paste(paste(names1,collapse='+', sep = ""),' ~ '
,paste(names2,collapse='+', sep = "")))
then we have to combine our input and output in a single data frame:
all_data = cbind(output, input)
then, use neuralnet like this:
net1 = neuralnet(formula = a, data = all_data, hidden=c(2,10))
plot(net1)
This is also work with the caret package:
net1 = train(a, data = all_data, method= "nnet", maxit = 1000)
but it seems neuralnet works faster (at least in my case).
I hope this helps someone else.

How to end a Basic function properly

I'm trying to write a function in Basic for LibreOffice Calc to get the first letter of each word of the selected cell using the following code:
Function GetFirstLetters(rng) As String
Dim arr
Dim I As Long
arr = Split(rng, " ")
If IsArray(arr) Then
For I = LBound(arr) To UBound(arr)
GetFirstLetters = GetFirstLetters & Left(arr(I), 1)
Next I
Else
GetFirstLetters = Left(arr, 1)
End If
End Function
And it works correctly, unless I try to execute it again, then it seems that the new result gets appended to that of any previous execution and it will return both strings together, example:
It also doesn't matter if I delete some or even all cells, or if I call it using an empty cell or even in another page, it will always append the result to the previous one:
Why does this happen? How can I fix this behaviour?
I don't know anything about Basic, so please don't bash on me if this is something very simple.
The original function is this:
Function GetFirstLetters(rng As Range) As String
'Update 20140325
Dim arr
Dim I As Long
arr = VBA.Split(rng, " ")
If IsArray(arr) Then
For I = LBound(arr) To UBound(arr)
GetFirstLetters = GetFirstLetters & Left(arr(I), 1)
Next I
Else
GetFirstLetters = Left(arr, 1)
End If
End Function
And I got it from here: http://www.extendoffice.com/documents/excel/1580-excel-extract-first-letter-of-each-word.html.
The code you have found is VBA for Excel. Openoffice or Libreoffice uses StarBasic, not VBA. This is similar but not equal. So you can't simply use the same code as in Excel.
First difference is, there is no Range object. This you have noticed and have used rng as an Variant.
But another difference is, function names are like variable names in the global scope. And they will not be reseted if the function is called again. So in StarBasic we better do:
Function GetFirstLetters(sCellValue as String) As String
Dim arr As Variant
Dim I As Long
Dim sResult As String
arr = Split(sCellValue, " ")
If IsArray(arr) Then
For I = LBound(arr) To UBound(arr)
sResult = sResult & Left(arr(I), 1)
Next I
Else
sResult = Left(arr, 1)
End If
GetFirstLetters = sResult
End Function
sResult is reseted (new Dimed) every time the function is called. So even the function's return value.

Selenium IDE: how to append int variable after string

I have just started using selenium IDE and I am trying to append a integer value after a constant string:
variable x = string
variable y = integer
append string+integer
anyone so kind to help me out?
I have never used java in my life.
Thank you!
You can store the values in another string like:
String name = "test";
int number = 1;
String combined = name+number;

TypeError: can't concat bytes to str

Im using Python3.4 but this project was imported from Python2 and the function below is displaying error mentiond in the title.
def int_to_bytes(i,len):
res = b""
for j in range(len):
res += chr(i%256)
i = i>>8
return res
I supposed to make a bytes value of result of chr() function?
Yes, you need to do this:
res += chr(i%256).encode('utf-8')
See also https://docs.python.org/3.4/library/stdtypes.html#str.encode

null plus null is sometimes 0, sometimes "nullnull"

In compiled mode.
String s1 = null;
String s2 = null;
s2 = s1 + s2;
Window.alert("null + null = " + s2);
result:
"null + null = nullnull"
But this piece of code:
public String getValue(DocListsInfo object) {
String s1 = object.getUrlForApplication();
Window.alert("2: " + s1);
String s2 = object.getEodLink();
Window.alert("3: " + s2);
String s3 = s1 + s2;
Window.alert("4: " + s3);
return s3;
}
=>
// compiled javascript:
function NUb(a){var b,c,d;b=a.q;$wnd.alert('2: '+b);c=a.g;$wnd.alert('3: '+c);d=b+c;$wnd.alert('4: '+d);return d}
returns 0 if both getters return null.
My insight into the inner workings of the GWT compiler is quite limited, but I think I might have an idea what's going on.
First of all, what's the difference between the two snippets? Every detail about the first one is known at compile time (both Strings are null) whereas we don't know much about the second one.
Now, what is null + null in Java? It's nullnull as can be read here.
However, what's null + null in JavaScript? It's 0 as you can simply test in your browser.
Now what I think is going on is the following: Let's start with your second code snippet. It is compiled to JavaScript, where JavaScript will then determine what s1 and s2 are based on object. It will then try to add/concatenate them and the result is 0. Well, that's ok, since that's what you would expect when you use JavaScript (well, not so much if you just know Java).
The first code snippet (and here comes my guess) seems to be optimized by a pre-/post-processing step of the compiler (since we already know they are both null). Consequently, all that JavaScript sees (what came out of the compiler) is a constant.
Maybe someone else can shed some more light on the step I guessed.