View Javadoc
1   package org.djutils.complex.demo;
2   
3   import org.djutils.complex.Complex;
4   import org.djutils.complex.ComplexMath;
5   
6   /**
7    * ComplexDemo.java. <br>
8    * <br>
9    * Copyright (c) 2021-2022 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
10   * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
11   * distributed under a three-clause BSD-style license, which can be found at
12   * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>. <br>
13   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
14   * @author <a href="https://www.tudelft.nl/pknoppers">Peter Knoppers</a>
15   */
16  public final class ComplexDemo
17  {
18      /**
19       * Do not instantiate.
20       */
21      private ComplexDemo()
22      {
23          // Do not instantiate
24      }
25  
26      /**
27       * Demonstrate the Complex class.
28       * @param args String[]; the command line arguments (not used).
29       */
30      public static void main(final String[] args)
31      {
32          constructors();
33          System.out.println();
34          fieldsGettersNormAndPhi();
35          System.out.println();
36          constants();
37          System.out.println();
38          binaryOps();
39          System.out.println();
40          mathFunctions();
41      }
42  
43      /**
44       * Constructors.
45       */
46      public static void constructors()
47      {
48          Complex z1 = new Complex(123.456, -345.678);
49          System.out.println(z1);
50          Complex z2 = new Complex(543.210); // Create a complex with imaginary component 0.0
51          System.out.println(z2);
52      }
53  
54      /**
55       * Fields, getters, norm and phi.
56       */
57      public static void fieldsGettersNormAndPhi()
58      {
59          Complex z = new Complex(123.456, -345.678);
60          System.out.println("re=" + z.re + ", im=" + z.im + ", getRe:" + z.getRe() + ", getIm:" + z.getIm() + ", norm:"
61                  + z.norm() + ", phi:" + z.phi());
62      }
63      
64      /**
65       * Constants.
66       */
67      public static void constants()
68      {
69          System.out.println("ZERO=" + Complex.ZERO);
70          System.out.println("ONE=" + Complex.ONE);
71          System.out.println("MINUS_ONE=" + Complex.MINUS_ONE);
72          System.out.println("I=" + Complex.I);
73          System.out.println("MINUS_I=" + Complex.MINUS_I);
74      }
75      
76      /**
77       * Binary operations.
78       */
79      public static void binaryOps()
80      {
81          Complex z1 = new Complex(3, 4);
82          Complex z2 = new Complex(-2, 5);
83          System.out.println("z1=" + z1);
84          System.out.println("z2=" + z2);
85          System.out.println("z1 + z2=" + z1.plus(z2));
86          System.out.println("z1 - z2=" + z1.minus(z2));
87          System.out.println("z1 * z2=" + z1.times(z2));
88          System.out.println("z1 / z2=" + z1.divideBy(z2));
89          System.out.println("z1 * I=" + z1.times(Complex.I));
90          System.out.println("I * I=" + Complex.I.times(Complex.I));
91      }
92  
93      /**
94       * Math functions.
95       */
96      public static void mathFunctions()
97      {
98          Complex z = new Complex(3, -5);
99          System.out.println(z);
100         System.out.println(ComplexMath.sqrt(z));
101         System.out.println(ComplexMath.sqrt(z).rotate(Math.PI));
102         System.out.println(ComplexMath.sqrt(z).times(-1));
103         System.out.println(ComplexMath.cbrt(z));
104         System.out.println(ComplexMath.cbrt(z).rotate(2 * Math.PI / 3));
105         System.out.println(ComplexMath.cbrt(z).rotate(4 * Math.PI / 3));
106         System.out.println(ComplexMath.exp(z));
107         System.out.println(ComplexMath.ln(z));
108         System.out.println(ComplexMath.sin(z));
109         System.out.println(ComplexMath.cos(z));
110         System.out.println(ComplexMath.tan(z));
111         System.out.println(ComplexMath.asin(z));
112         System.out.println(ComplexMath.acos(z));
113         System.out.println(ComplexMath.atan(z));
114         System.out.println(ComplexMath.sinh(z));
115         System.out.println(ComplexMath.cosh(z));
116         System.out.println(ComplexMath.tanh(z));
117         System.out.println(ComplexMath.asinh(z));
118         System.out.println(ComplexMath.acosh(z));
119         System.out.println(ComplexMath.atanh(z));
120     }
121     
122 }