Class JxnLoop


  • public class JxnLoop
    extends Object
    Collection of static methods used to repeat commands. The commands are executed on the KmgFormelInterpreter instance passed to the parameter fi. ((If the commands must be executed on an independent instance, @KmgFormelInterpreter($this) can be passed instead of $this.))
    • Method Detail

      • fill

        public static Object fill​(KmgFormelInterpreter fi,
                                  String var,
                                  int n,
                                  String cmd)
        Executes cmd for values var = 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 of KmgFormelInterpreter on which cmd is executed
        var - name of the variable to be used in cmd
        n - number of repetitions
        cmd - statement_sequence (one or more statements separated by ';') or #filename[.jxn]. If cmd 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)
        Executes cmd for each element of arrayOrIterator.
        Example:
            oa = JxnLoop.fill( $this, "$arg", { t, j, 3. }, "sqrt $arg;" );
            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)
         
        Note: In many cases the auto array feature of JXN already gives the desired result:
        For example with
            x = { 2., 3., 5., 7. }
             = { 2.0, 3.0, 5.0, 7.0 } (double[4])
        simply use
            y = sqrt x
             = { 1.4142135623730951, 1.7320508075688772 ... 2.6457513110645907 } (double[4])
        instead of
            y = JxnLoop.fill( $this, "$xi", x, "sqrt $xi;" )
             = { 1.4142135623730951, 1.7320508075688772 ... 2.6457513110645907 } (double[4])     
         
        Parameters:
        fi - instance of KmgFormelInterpreter on which cmd is executed
        var - name of the variable used in cmd to access the consecutive elements of arrayOrIterator
        arrayOrIterator - array of any type or a java.util.Iterator or an object of a class which implements a toArray() method
        cmd - statement_sequence (one or more statements separated by ';') or #filename[.jxn]. If cmd 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)
        Executes cmd for values var = 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 of KmgFormelInterpreter on which cmd is executed
        var - name of the variable to be used in cmd
        n - number of repetitions
        cmd - statement_sequence (one or more statements separated by ';') or #filename[.jxn]. If cmd 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)
        Executes  cmd while  whileCondition is  true or not  0 or not  null or a char from  1TtYy .
        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 of KmgFormelInterpreter on which whileCondition and cmd are executed
        whileCondition - (single statement) checked before each repetition of cmd
        cmd - statement_sequence (one or more statements separated by ';') or #filename[.jxn]. If cmd 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)
        Executes cmd for each element of arrayOrIterator.
        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 of KmgFormelInterpreter on which cmd is executed
        var - name of the variable used in cmd to access the consecutive elements of arrayOrIterator
        arrayOrIterator - array of any type or a java.util.Iterator or an object of a class which implements a toArray() method
        cmd - statement_sequence (one or more statements separated by ';') or #filename[.jxn]. If cmd ends with ';', log output of intermediate steps is suppressed
        Returns:
        result of the last statement executed
        See Also:
        Iterable.iterator(), Enumeration.asIterator(), Collection.toArray()