Class JxnLoop
- java.lang.Object
-
- JxnLoop
-
public class JxnLoop extends Object
Collection of static methods used to repeat commands. The commands are executed on theKmgFormelInterpreter
instance passed to the parameterfi
. ((If the commands must be executed on an independent instance,@KmgFormelInterpreter($this)
can be passed instead of$this
.))
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Object
fill(KmgFormelInterpreter fi, String var, int n, String cmd)
Executescmd
for valuesvar = 0 ... n-1
.static Object
fill(KmgFormelInterpreter fi, String var, Object array, String cmd)
Executescmd
for each element ofarray
.static Object
foreach(KmgFormelInterpreter fi, String var, Object array, String cmd)
Executescmd
for each element ofarray
.static Object
repeat(KmgFormelInterpreter fi, String var, int n, String cmd)
Executescmd
for valuesvar = 0 ... n-1
.static Object
repeat(KmgFormelInterpreter fi, String whileCondition, String cmd)
Executescmd
whilewhileCondition
is true.
-
-
-
Method Detail
-
fill
public static Object fill(KmgFormelInterpreter fi, String var, int n, String cmd)
Executescmd
for valuesvar = 0 ... n-1
.
Example:ao = JxnLoop.fill( $this, "$i", 5, "sqrt $i;" ) = { 0.0, 1.0, 1.4142135623730951, 1.7320508075688772, 2.0 } (double[5])
Note: In many cases the auto array feature of JXN already gives the desired result.
For example instead of the above simply use:ao = sqrt { 0 : 4 } = { 0.0, 1.0, 1.4142135623730951, 1.7320508075688772, 2.0 } (double[5])
- Parameters:
fi
- instance ofKmgFormelInterpreter
on whichcmd
is executedvar
- name of the variable to be used incmd
n
- number of repetitionscmd
- statement_sequence (one or more statements separated by';'
) or #filename[.jxn]. Ifcmd
ends with ';
', log output of intermediate steps is suppressed- Returns:
- array(object) containing the results of the successive executions of
cmd
(both a java primitive array (e.g. double []) or an object array (e.g. String[]) can be returned as Object)
-
fill
public static Object fill(KmgFormelInterpreter fi, String var, Object array, String cmd)
Executescmd
for each element ofarray
.
Example:oa = JxnLoop.fill( $this, "$arg", { t, j, 3. }, "sqrt $arg;" );
Note: In many cases the auto array feature of JXN already gives the desired result:format
oa = { {0.0, 0.044721359549995794, ... 1.0} (JxnRealArrayAlgebra), = 0.7071067811865476 + j 0.7071067811865475 = 1.0 <) 45.0° (JxnComplexAlgebra), = 1.7320508075688772 (double) } (java.lang.Object[3]) (java.lang.String)
For example withx = { 2., 3., 4., 5. } = { 2.0, 3.0, ... 5.0 } (double[4])
simply usey = sqrt x = { 1.4142135623730951, 1.7320508075688772, ... 2.23606797749979 } (double[4])
instead ofy = JxnLoop.fill( $this, "$xi", x, "sqrt $xi;" ) = { 1.4142135623730951, 1.7320508075688772, ... 2.23606797749979 } (double[4])
- Parameters:
fi
- instance ofKmgFormelInterpreter
on whichcmd
is executedvar
- name of the variable used incmd
to access the consecutive elements ofarray
array
- array of any type or object of a class which implements atoArray()
method.cmd
- statement_sequence (one or more statements separated by';'
) or #filename[.jxn]. Ifcmd
ends with ';
', log output of intermediate steps is suppressed- Returns:
- array(object) containing the results of the successive executions of
cmd
(both a java primitive array (e.g. double []) or an object array (e.g. String[]) can be returned as Object)
-
repeat
public static Object repeat(KmgFormelInterpreter fi, String var, int n, String cmd)
Executescmd
for valuesvar = 0 ... n-1
.
Example:factorial = 1; JxnLoop.repeat( $this, "$i", 5, "factorial = factorial * ( $i + 1 )" ) : factorial = factorial * ( $i + 1 ) : = 1 (int) : factorial = factorial * ( $i + 1 ) : = 2 (int) : factorial = factorial * ( $i + 1 ) : = 6 (int) : factorial = factorial * ( $i + 1 ) : = 24 (int) : factorial = factorial * ( $i + 1 ) : = 120 (int) = 120 (int)
- Parameters:
fi
- instance ofKmgFormelInterpreter
on whichcmd
is executedvar
- name of the variable to be used incmd
n
- number of repetitionscmd
- statement_sequence (one or more statements separated by';'
) or #filename[.jxn]. Ifcmd
ends with ';
', log output of intermediate steps is suppressed- Returns:
- result of the last statement executed
-
repeat
public static Object repeat(KmgFormelInterpreter fi, String whileCondition, String cmd)
Executescmd
whilewhileCondition
is true.
Example:i0 = 0; i1 = 1; JxnLoop.repeat( $this, "
le
( i2, 25 )", "i2 = i0 + i1; i0 = i1; i1 = i2" ) : i2 = i0 + i1; i0 = i1; i1 = i2 : = 1 (int) : i2 = i0 + i1; i0 = i1; i1 = i2 : = 2 (int) : i2 = i0 + i1; i0 = i1; i1 = i2 : = 3 (int) : i2 = i0 + i1; i0 = i1; i1 = i2 : = 5 (int) : i2 = i0 + i1; i0 = i1; i1 = i2 : = 8 (int) : i2 = i0 + i1; i0 = i1; i1 = i2 : = 13 (int) : i2 = i0 + i1; i0 = i1; i1 = i2 : = 21 (int) = 21 (int)- Parameters:
fi
- instance ofKmgFormelInterpreter
on whichwhileCondition
andcmd
are executedwhileCondition
- (single statement) checked before each repetition ofcmd
cmd
- statement_sequence (one or more statements separated by';'
) or #filename[.jxn]. Ifcmd
ends with ';
', log output of intermediate steps is suppressed- Returns:
- result of the last statement executed
-
foreach
public static Object foreach(KmgFormelInterpreter fi, String var, Object array, String cmd)
Executescmd
for each element ofarray
.
Example:sum = 0; JxnLoop.foreach( $this, "$i", { 2 : 5 }, "sum = sum + $i;" ) = 14 (int)
- Parameters:
fi
- instance ofKmgFormelInterpreter
on whichcmd
is executedvar
- name of the variable used incmd
to access the consecutive elements ofarray
array
- array of any type or object of a class which implements atoArray()
method.cmd
- statement_sequence (one or more statements separated by';'
) or #filename[.jxn]. Ifcmd
ends with ';
', log output of intermediate steps is suppressed- Returns:
- result of the last statement executed
-
-