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 arrayOrIterator, String cmd)
Executescmd
for each element ofarrayOrIterator
.static Object
foreach(KmgFormelInterpreter fi, String var, Object arrayOrIterator, String cmd)
Executescmd
for each element ofarrayOrIterator
.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
istrue
or not0
or notnull
or a char from1TtYy
.
-
-
-
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) of length n 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 arrayOrIterator, String cmd)
Executescmd
for each element ofarrayOrIterator
.
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., 5., 7. } = { 2.0, 3.0, 5.0, 7.0 } (double[4])
simply usey = sqrt x = { 1.4142135623730951, 1.7320508075688772 ... 2.6457513110645907 } (double[4])
instead ofy = JxnLoop.fill( $this, "$xi", x, "sqrt $xi;" ) = { 1.4142135623730951, 1.7320508075688772 ... 2.6457513110645907 } (double[4])
- Parameters:
fi
- instance ofKmgFormelInterpreter
on whichcmd
is executedvar
- name of the variable used incmd
to access the consecutive elements ofarrayOrIterator
arrayOrIterator
- array of any type or ajava.util.Iterator
or an object of a class which implements atoArray()
methodcmd
- 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) - See Also:
Iterable.iterator()
,Enumeration.asIterator()
,Collection.toArray()
-
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 *= $i + 1" ) : factorial *= $i + 1 : = 1 (int) : factorial *= $i + 1 : = 2 (int) : factorial *= $i + 1 : = 6 (int) : factorial *= $i + 1 : = 24 (int) : 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
istrue
or not0
or notnull
or a char from1TtYy
.
Examples:! fibonacci numbers: f0 = 0; f1 = 1; JxnLoop.repeat( $this, "
le
( f1, 20 )", "f2 = f0 + f1; f0 = f1; f1 = f2" ) : f2 = f0 + f1; f0 = f1; f1 = f2 : = 1 (int) : f2 = f0 + f1; f0 = f1; f1 = f2 : = 2 (int) : f2 = f0 + f1; f0 = f1; f1 = f2 : = 3 (int) : f2 = f0 + f1; f0 = f1; f1 = f2 : = 5 (int) : f2 = f0 + f1; f0 = f1; f1 = f2 : = 8 (int) : f2 = f0 + f1; f0 = f1; f1 = f2 : = 13 (int) : f2 = f0 + f1; f0 = f1; f1 = f2 : = 21 (int) = 21 (int) ! determine the number of characters in a file: br = @BufferedReader
@FileReader
fileDialog()
= java.io.BufferedReader@4b9e13df (java.io.BufferedReader) cnt = 0; JxnLoop.repeat( $this, "line = br.readLine()
", "cnt += line.length()
;" ) = 2101 (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 arrayOrIterator, String cmd)
Executescmd
for each element ofarrayOrIterator
.
Example:sum = 0; JxnLoop.foreach( $this, "$i", 3 + 3 { 0 : 4 }, "sum += $i; $i + \": \" + sum" ); : sum += $i; $i + ": " + sum : = 3: 3 (java.lang.String) : sum += $i; $i + ": " + sum : = 6: 9 (java.lang.String) : sum += $i; $i + ": " + sum : = 9: 18 (java.lang.String) : sum += $i; $i + ": " + sum : = 12: 30 (java.lang.String) : sum += $i; $i + ": " + sum : = 15: 45 (java.lang.String) sum = 45 (int)
- Parameters:
fi
- instance ofKmgFormelInterpreter
on whichcmd
is executedvar
- name of the variable used incmd
to access the consecutive elements ofarrayOrIterator
arrayOrIterator
- array of any type or ajava.util.Iterator
or an object of a class which implements atoArray()
methodcmd
- 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
- See Also:
Iterable.iterator()
,Enumeration.asIterator()
,Collection.toArray()
-
-