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

后端 未结 7 850
广开言路
广开言路 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:34

    Use Apache Common Math. It is very easy to use.

    You can initialize both real+imaginary parts. You can also initialize them from a string. It supports a wide array of operations that you can do with imaginary numbers.

    Here is a example of code for doing some common operations:

    package complex;
    import static java.lang.String.format;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import org.apache.commons.math3.complex.Complex;
    import org.apache.commons.math3.complex.ComplexFormat;
    public class Do 
    {
        public static void main(String[] args) 
    {
         ComplexFormat format = new ComplexFormat();
        Complex lhs = new Complex(1.0, 3.0);
        Complex rhs = new Complex(2.0, 5.0);
    
        Complex answer = lhs.add(rhs);       // add two complex numbers
        System.out.println("Add : "+ format.format(answer));
        answer = lhs.subtract(rhs);  // subtract two complex numbers
        System.out.println("Subtract : "+ format.format(answer));
        answer = lhs.conjugate();
        System.out.println("Conjgate : "+ format.format(answer));
        double d = lhs.abs();
        System.out.println("Absolute : "+d);
        Complex first  = new Complex(1.0, 3.0);
        Complex second = new Complex(2.0, 5.0);
    
        answer = first.log();        // natural logarithm.
                System.out.println("Logarithm : "+ format.format(answer));
        answer = first.cos();        // cosine
                System.out.println("Cosine : "+ format.format(answer));
        answer = first.pow(second);  // first raised to the power of second
                System.out.println("Power : "+ format.format(answer));
    
                Complex z = new Complex(2.0,2.0);
                Complex z1 = z.reciprocal();
                System.out.println("Recipocal : "+ format.format(z1));
    
                System.out.println("Absoltue of 2+2i is "+z.abs());
                System.out.println("Argument of 2+2i is "+z.getArgument());
    
        Complex r = new Complex(6.3,9.6);
        String conj = format.format(r.conjugate());
        String reci = format.format(r.reciprocal());
    
        System.out.println("Conjugate : "+conj+" Recipocal : "+reci);
    
        //answer = lhs.abs();          // absolute value
        //answer = lhs.conjugate(rhs); // complex conjugate
    
        //make complex to string
    
        ComplexFormat format = new ComplexFormat(); // default format
        Complex c = new Complex(1.1111, 2.2222);
        String s = format.format(c); // s contains "1.11 + 2.22i"
        System.out.println(s);
    
        //make string to complex
    
        String z = "2.5+3.6i";
        Complex e = format.parse(z);
        System.out.println(e);
    
    }    
    }
    

    Another alternative is FrAid, if you want another option.

提交回复
热议问题