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) 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)
        Executes cmd for each element of array.
        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., 4., 5. }
             = { 2.0, 3.0, ... 5.0 } (double[4])
        simply use
            y = sqrt x
             = { 1.4142135623730951, 1.7320508075688772, ... 2.23606797749979 } (double[4])
        instead of
            y = JxnLoop.fill( $this, "$xi", x, "sqrt $xi;" )
             = { 1.4142135623730951, 1.7320508075688772, ... 2.23606797749979 } (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 array
        array - array of any type or 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)
      • 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 = 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 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.
        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 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 array,
                                     String cmd)
        Executes cmd for each element of array.
        Example:
            sum = 0; JxnLoop.foreach( $this, "$i", { 2 : 5 }, "sum = sum + $i;" )
             = 14 (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 array
        array - array of any type or 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