Java math expression parser that can take complex numbers as a variable?

后端 未结 7 846
广开言路
广开言路 2021-01-21 10:58

I am writing a program in Processing that transforms complex numbers. However, I want to have a method of taking an input string and calculating the transformation using a compl

相关标签:
7条回答
  • 2021-01-21 11:49

    As mentioned by PhiLo, you can use generics. Try this Processing sketch:

    import java.util.*;
    java.util.List<String> list = Arrays.asList("a", "b", "c");
    textFont(loadFont("UMingCN-30.vlw"));
    for(int i = 0; i < list.size(); i++) {
      text(list.get(i), 5, int(i*30)+30);
    }
    

    And there's a non commercial version of JEP available (GPL). Download it here and add it to your Processing classpath (import it). After successfully doing so, you can use JEP like this:

    void setup() {
      org.nfunk.jep.JEP parser = new org.nfunk.jep.JEP();
      parser.addComplex();
      try {
        parser.parseExpression("(1+2*i) + (3+8*i)");
        println(parser.getComplexValue());
      } catch(Exception e) {
        e.printStackTrace();
      }
    }
    

    which produces the (expected) output: (4.0, 10.0)

    0 讨论(0)
提交回复
热议问题