Syntax Operators

Hello,
is there a way to use less than <; bigger than > operators in AnyBody?
It is for a differentiation of the supporting moment of an exoskeleton actuator.
Best regards
Christoph

1 Like

Hi Christoph.

It may not work in all circumstances, but AnyBody does have a system for this. It is very much like in excel functions where all operations are functions, which are nested inside each other. Here is an example combining iffun() and gtfun() (i.e. >)

AnyVar SomeVariable = iffun( gtfun(OtherVariable, 10), <value_if_true>, <value_if_false>);

The functions are

  • <: ltfun(x,y)
  • >: gtfun(x,y)
  • >=: gteqfun(x,y)
  • <=: lteqfun(x,y)
  • ==: eqfun(x,y)
  • !=: neqfun(x,y)

Here some examples from the AnyScript Reference demos:

/** 
Examples of using logical/conditional functions

We demonstrate the following functions

* iffun(condition,true-return,false-return)

* logical functions: 

  * Equality testing: ``eqfun(x1,x2)``
  * Not-equal testing: ``neqfun(x1,x2)``
  * Greater and smaller than testing: ``gtfun(x1,x2)``, ``gteqfun(x1,x2)``, ``ltfun(x1,x2)``, ``lteqfun(x1,x2)``
  * and/or functions: ``andfun(x1,x2)``, ``orfun(x1,x2)``

Handling of multi-element and multi-dimensional arguments is treated 
in separte sections in the end.
*/
Main = {
  
    
  /// Equality testing
  AnyFolder Equalities = {
    AnyIntVar test1 = eqfun(1.1,0.0);
    AnyIntVar test2 = eqfun(10,10);
    AnyIntVar test3 = eqfun("dsdas","cscd");
    AnyIntVar test4 = eqfun("anybody","anybody");
    AnyIntVar test5 = eqfun(On, On);
    AnyIntVar test6 = eqfun(MR_Quadratic, MR_Quadratic);
    AnyIntVar test7 = neqfun(&On, &On);
};
  
  /// Not-equal testing
  AnyFolder NotEqualities = {
    AnyIntVar test1 = neqfun(1.1,0.0);
    AnyIntVar test2 = neqfun(10,10);
    AnyIntVar test3 = neqfun("dsdas","cscd");
    AnyIntVar test4 = neqfun("anybody","anybody");
    AnyIntVar test5 = neqfun(On, Off);
    AnyIntVar test6 = neqfun(Hard, Soft);
    AnyIntVar test7 = neqfun(&On, &Off);

  };

  /// Not-equal testing by the not(..) function
  AnyFolder NotEqualities2 = {
    AnyIntVar test1 = not(eqfun(1.1,0.0));
    AnyIntVar test2 = not(eqfun(10,10));
    AnyIntVar test3 = not(eqfun("dsdas","cscd"));
    AnyIntVar test4 = not(eqfun("anybody","anybody"));
    AnyIntVar test5 = not(Off);
  };
  
  
  /// Inequality testing
  AnyFolder Inequalities = {
    AnyIntVar lt1 = ltfun(-1.1,0.0);
    AnyIntVar lt2 = ltfun(10,10);
    AnyIntVar lt3 = ltfun(8,10);
    AnyIntVar lt4 = ltfun(10.0,8.0);
    
    AnyIntVar lteq1 = lteqfun(-1.1,0.0);
    AnyIntVar lteq2 = lteqfun(10,10);
    AnyIntVar lteq3 = lteqfun(8,10);
    AnyIntVar lteq4 = lteqfun(10.0,8.0);

    AnyIntVar gt1 = gtfun(-1.1,0.0);
    AnyIntVar gt2 = gtfun(10,10);
    AnyIntVar gt3 = gtfun(8,10);
    AnyIntVar gt4 = gtfun(10.0,8.0);

    AnyIntVar gteq1 = gteqfun(-1.1,0.0);
    AnyIntVar gteq2 = gteqfun(10,10);
    AnyIntVar gteq3 = gteqfun(8,10);
    AnyIntVar gteq4 = gteqfun(10.0,8.0);
  };

  // Not function
  AnyFolder Not = {
    AnyInt not_string = {not("hello"), not("")};
    AnyInt not_int = {not(1), not(0)};
    AnyInt not_float = {not(1.2), not(0.0)};
    AnyInt not_swtich = {not(On), not(Off)}; 
    AnyInt not_objectptr = {not(&On), not(&On), not(None)};
  };
  
  AnyFolder Bool = {
    AnyInt bool_string = {bool("hello"), bool("")};
    AnyInt bool_int = {bool(1), bool(0)};
    AnyInt bool_float = {bool(1.2), bool(0.0)};
    AnyInt bool_swtich = {bool(On), bool(Off)}; 
    AnyInt bool_objectptr = bool({&On, &Off, None});
  };
  
  /// Combined tests using 'and' and 'or' functions
  AnyFolder AndOr = {
    AnyIntVar and1 = andfun(1,1);
    AnyIntVar and2 = andfun(1.0,0.0);
    AnyIntVar and3 = andfun(0.0,0.0);
    AnyIntVar and4 = andfun(On,On);

    AnyIntVar or1 = orfun(1,1);
    AnyIntVar or2 = orfun(1.0,0.0);
    AnyIntVar or3 = orfun(0.0,0.0);
    AnyIntVar or4 = orfun(Off,Off);

  };
  
  /// if-then-else function
  AnyFolder IfThenElse = {
    AnyVar A = 1;
    AnyVar B = 2;
    AnyVar C = 3;
    AnyVar D = 4;
    
    AnyStringVar ifanswer1 = iffun(ltfun(A,B), "A is smaller than B", "A is not smaller than B");

    AnyStringVar ifanswer2 = ifanswer1 + " and " + iffun( andfun(ltfun(A,B),gtfun(C,D)), "C is greater than D", "C is not greater than D");    
  };
  
  /// logical functions for arrays, matrices or higher dimensionalities
  AnyFolder If_MultiDim = {

    AnyVector test1D = {1,0,1,0,1,0,1,1,0};
    AnyVector true1D = {1,1,1,1,1,1,1,1,1};
    AnyVector false1D = 0*true1D;
    AnyMatrix test2D = {test1D, true1D, false1D};
    
    /// Multi-dimensional condition w. expanded scalar floating-point return
    AnyFloat ifanswer1float = iffun(test2D, 2.0, 0.0);     
    /// Multi-dimensional condition w. expanded scalar string return
    AnyString ifanswer1str = iffun(test2D, "One", "Zero"); 

    /// Multi-dimensional condition w. combined multi-element and expanded-scalar return
    /// Notice that first (true) return argument has smaller size than the condition, thus it determines the output
    AnyFloat answer2 = iffun(test2D', true1D, 0.0);
    
    
    /// Multi-element condition w. multi-element return arguments
    /// Notice that condition determines the return size as it is the smaller size argument
    AnyFloat answer3 = iffun( {0.0,1.0},true1D, false1D);

    /// Scalar condition w. multi-element return arguments
    /// Notice that condition is treated as if it was expanded to match the return arguments
    /// and that the smaller size return argument determines the return value size
    AnyFloat answer4 = iffun( 0.0, {{true1D},{true1D}}, false1D);

    
    // If functions using logical comparison functions with multi-element argument
    AnyString answer5 = iffun( eqfun(test1D,true1D), "True", "False");

  
  };
  
  
  /// Other logical functions for arrays, matrices or higher dimensionalities
  AnyFolder Comparison_MultiDim = {
    AnyIntArray arr_ones = {1,1,1,1,1,1,1,1,1};
    AnyIntArray arr_zeros = 0*arr_ones;
    AnyVector arr1 = {1,0,1,0,1,0,1,1,0};
    
    AnyInt or1 = orfun(arr_ones,arr1);
    AnyInt and1 = andfun(arr_ones,arr1);
    AnyInt or2 = orfun(arr_zeros,arr1);
    AnyInt and2 = andfun(arr_zeros,arr1);
    
    AnyMatrix mat1 = {arr1, arr_ones, arr_zeros };
    AnyMatrix mat2 = {arr1, arr1 };
    
    AnyInt eq1 = eqfun(mat1,mat2); ///< Notice how results are returned only for the argument with fewer elements
    AnyInt eq2 = eqfun(mat1,mat2'); ///< Notice how results are returned only for the intersecting entries of the arguments
    
    AnyInt eq_array_vs_scalar = eqfun(mat1,1); ///< Notice how a scalar argument (here 2nd argument) is expanded to match the a higher dimensional element.
  };
  
  
    
  
  
};  // Main
1 Like

The reason I wrote that it **"may not work in all circumstances"*, is that some values needs to be constant and are not allowed to change by an expression.

This topic was automatically closed 125 days after the last reply. New replies are no longer allowed.