/*
*   Class   Phasor
*
*   Defines a Phasor and includes
*   the methods needed for standard Phasor arithmetic
*
*   See PhasorMatrix for creation and manipulatioin of matrices of phasors
*
*   WRITTEN BY: Dr Michael Thomas Flanagan
*
*   DATE:    4 July 2007
*   AMENDED: 17 april 2008, 1-5 December 2011
*
*   DOCUMENTATION:
*   See Michael T Flanagan's Java library on-line web pages:
*   http://www.ee.ucl.ac.uk/~mflanaga/java/
*   http://www.ee.ucl.ac.uk/~mflanaga/java/Phasor.html
*
*   Copyright (c) 2007 - 2008    Michael Thomas Flanagan
*
* PERMISSION TO COPY:
*
* Permission to use, copy and modify this software and its documentation for NON-COMMERCIAL purposes is granted, without fee,
* provided that an acknowledgement to the author, Dr Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies
* and associated documentation or publications.
*
* Redistributions of the source code of this source code, or parts of the source codes, must retain the above copyright notice, this list of conditions
* and the following disclaimer and requires written permission from the Michael Thomas Flanagan:
*
* Redistribution in binary form of all or parts of this class must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution and requires written permission from the Michael Thomas Flanagan:
*
* Dr Michael Thomas Flanagan makes no representations about the suitability or fitness of the software for any or for a particular purpose.
* Dr Michael Thomas Flanagan shall not be liable for any damages suffered as a result of using, modifying or distributing this software
* or its derivatives.
*
***************************************************************************************/

package flanagan.circuits;

import flanagan.complex.Complex;
import flanagan.math.Fmath;
import flanagan.math.VectorMaths;

public class Phasor{

        private double magnitude = 0.0D;            // magnitude of the Phasor
        private double phaseInDeg = 0.0D;           // phase of the Phasor in radians
        private double phaseInRad = 0.0D;           // phase of the Phasor in degrees
        private Complex rectangular = new Complex(0.0, 0.0);    // rectangular complex equivalent of the Phasor

        // frequency - static to prevent inappropriate combination of phasors
        private static double frequency = Double.NaN;   // frequency in Hz
        private static double omega = Double.NaN;       // radial frequency



        // CONSTRUCTORS
        // default constructor
        public Phasor(){
        }

        // Constructor setting magnitude and phase in degrees
        public Phasor(double magnitude, double phase){
                this.magnitude = magnitude;
                this.phaseInDeg = phase;
                this.phaseInRad = Math.toRadians(phase);
                this.rectangular.polar(this.magnitude, this.phaseInRad);
        }

        // Constructor setting magnitude only
        public Phasor(double magnitude){
                this.magnitude = magnitude;
                this.rectangular.polar(this.magnitude, this.phaseInRad);
        }

        // SET VALUES
        // Set the frequency in Hz - as a static variable
        public static void setFrequency(double freq){
                if(Fmath.isNaN(Phasor.frequency)){
                    Phasor.frequency = freq;
                    Phasor.omega = 2.0D*Math.PI*freq;

                }
                else{
                    throw new IllegalArgumentException("You have already entered a value for the frequency, " + Phasor.frequency + ", that differs from the one you are now attempting to enter, " + freq);
                }
        }

        // Set the radial frequency - as a static variable
        public static void setRadialFrequency(double omega){
                if(Fmath.isNaN(Phasor.omega)){
                    Phasor.omega = omega;
                    Phasor.frequency = Phasor.omega/(2.0D*Math.PI);
                }
                else{
                    throw new IllegalArgumentException("You have already entered a value for the radial frequency, omega, " + Phasor.omega + ", that differs from the one you are now attempting to enter, " + omega);
                }
        }

        // Set the value of magnitude
        public void setMagnitude(double magnitude){
                this.magnitude = magnitude;
                this.rectangular.polar(this.magnitude, this.phaseInRad);
        }

        // Set the value of phase in degrees
        public void setPhaseInDegrees(double phase){
                this.phaseInDeg = phase;
                this.phaseInRad = Math.toRadians(phase);
                this.rectangular.polar(this.magnitude, this.phaseInRad);
        }


        // Set the values of magnitude and phase in degrees
        public void reset(double magnitude, double phaseInDegrees){
                this.magnitude = magnitude;
                this.phaseInDeg = phaseInDegrees;
                this.phaseInRad = Math.toRadians(phaseInDegrees);
                this.rectangular.polar(this.magnitude, this.phaseInRad);
        }


        // GET VALUES
        // Get the frequency in Hz
        public static double getFrequency(){
                return Phasor.frequency;
        }

        // Get the radial frequency
        public static double setRadialFrequency(){
                return Phasor.omega;
        }

        // Get the value of magnitude
        public double getMagnitude(){
                return this.magnitude;
        }

        // Get the value of phase in degrees
        public double getPhaseInDegrees(){
                return this.phaseInDeg;
        }

        // Get the value of phase in radians
        public double getPhaseInRadians(){
                return this.phaseInRad;
        }

        // Get the real part of the Phasor
        public double getReal(){
            return this.magnitude*Math.cos(this.phaseInRad);
        }

        // Get the imaginary part of the Phasor
        public double getImag(){
            return this.magnitude*Math.sin(this.phaseInRad);
        }

        // CONVERSIONS

        // converts rectangular complex variable to a Phasor
        public static Phasor toPhasor(Complex cc){
                Phasor ph = new Phasor();
                ph.magnitude = cc.abs();
                ph.phaseInRad = cc.argRad();
                ph.phaseInDeg = cc.argDeg();
                ph.rectangular = cc;
                return ph;
        }

        // converts the Phasor to a rectangular complex variable
        public Complex toRectangular(){
                Complex cc = new Complex();
                cc.polar(this.magnitude, this.phaseInRad);
                return cc;
        }

        // converts the Phasor to a rectangular complex variable - static method
        public static Complex toRectangular(Phasor ph){
                Complex cc = new Complex();
                cc.polar(ph.magnitude, ph.phaseInRad);
                return cc;
        }

        // converts the Phasor to a rectangular complex variable
        public Complex toComplex(){
                Complex cc = new Complex();
                cc.polar(this.magnitude, this.phaseInRad);
                return cc;
        }

        // converts the Phasor to a rectangular complex variable - static method
        public static Complex toComplex(Phasor ph){
                Complex cc = new Complex();
                cc.polar(ph.magnitude, ph.phaseInRad);
                return cc;
        }
        
        // converts the phasor to VectorMaths vector = instance method
        public VectorMaths toVectorMaths(){
            double x = this.magnitude*Math.sin(this.phaseInRad); 
            double y = this.magnitude*Math.cos(this.phaseInRad); 
            VectorMaths vec = new VectorMaths(x, y);
            return vec;
        }
        
        // converts the phasor to VectorMaths vector = instance method
        public static VectorMaths toVectorMaths(Phasor ph){
            double x = ph.magnitude*Math.sin(ph.phaseInRad); 
            double y = ph.magnitude*Math.cos(ph.phaseInRad); 
            VectorMaths vec = new VectorMaths(x, y);
            return vec;
        }

        // Format a phasor number as a string, 'magnitude''<''phase''deg' - phase in degrees
        // Overides java.lang.String.toString()
        // instance method
        public String toString(){
                return this.magnitude + "<" + this.phaseInDeg + "deg";
        }

        // Format a phasor number as a string, 'magnitude''<''phase''deg' - phase in degrees
        // Overides java.lang.String.toString()
        // static method
        public static String toString(Phasor ph){
                return ph.magnitude + "<" + ph.phaseInDeg + "deg";
        }


        // Parse a string to obtain Phasor
        // accepts strings:
        // 'magnitude''<''phase' - phase in degrees
        // 'magnitude''L''phase' - phase in degrees
        // 'magnitude''<''phase''deg' - phase in degrees
        // 'magnitude''L''phase''deg' - phase in degrees
        // 'magnitude''<''phase''rad' - phase in radians
        // 'magnitude''L''phase''rad' - phase in radians
        public static Phasor parsePhasor(String ss){
                Phasor ph = new Phasor();
                ss = ss.trim();
                int anglePos = ss.indexOf('<');
                if(anglePos==-1){
                    anglePos = ss.indexOf('L');
                    if(anglePos==-1)throw new IllegalArgumentException("no angle symbol, <, in the string, ss");
                }
                int degPos = ss.indexOf('d');
                if(degPos==-1)degPos = ss.indexOf('D');
                int radPos = ss.indexOf('r');
                if(radPos==-1)degPos = ss.indexOf('R');
                String mag = ss.substring(0,anglePos);
                ph.magnitude = Double.parseDouble(mag);
                String phas = null;
                if(degPos!=-1){
                    phas = ss.substring(anglePos+1, degPos);
                    ph.phaseInDeg = Double.parseDouble(mag);
                    ph.phaseInRad = Math.toRadians(ph.phaseInDeg);
                }
                if(degPos==-1 && radPos==-1){
                    phas = ss.substring(anglePos+1);
                    ph.phaseInDeg = Double.parseDouble(phas);
                    ph.phaseInRad = Math.toRadians(ph.phaseInDeg);
                }
                if(radPos!=-1){
                    phas = ss.substring(anglePos+1, radPos);
                    ph.phaseInRad = Double.parseDouble(phas);
                    ph.phaseInDeg = Math.toDegrees(ph.phaseInRad);
                }
                ph.rectangular.polar(ph.magnitude, ph.phaseInRad);

                return ph;
        }

        // Same method as parsePhasor
        // Overides java.lang.Object.valueOf()
        public static Phasor valueOf(String ss){
                return Phasor.parsePhasor(ss);
        }

        // INPUT AND OUTPUT

        // READ A PHASOR
        // Read a Phasor from the keyboard console after a prompt message
        // in a String format compatible with Phasor.parse,
        // 'magnitude'<'phase', 'magnitude'<'phase'deg, 'magnitude'<'phase'rad
        // e.g. 1.23<34.1deg, -0.67<-56.7, 6.8e2<-0.22rad
        // prompt = Prompt message to vdu
        public static final synchronized Phasor readPhasor(String prompt){
                int ch = ' ';
                String phstring = "";
                boolean done = false;

                System.out.print(prompt + " ");
                System.out.flush();

                while (!done){
                        try{
                                ch = System.in.read();
                                if (ch < 0 || (char)ch == '\n')
                                        done = true;
                                else
                                        phstring = phstring + (char) ch;
                        }
                        catch(java.io.IOException e){
                                done = true;
                        }
                }
                return Phasor.parsePhasor(phstring);
        }

        // Read a Phasor from the keyboard console after a prompt message (with String default option)
        // in a String format compatible with Phasor.parse,
        // 'magnitude'<'phase', 'magnitude'<'phase'deg, 'magnitude'<'phase'rad
        // e.g. 1.23<34.1deg, -0.67<-56.7, 6.8e2<-0.22rad
        // prompt = Prompt message to vdu
        // dflt = default value
        public static final synchronized Phasor readPhasor(String prompt, String dflt){
                int ch = ' ';
                String phstring = "";
                boolean done = false;

                System.out.print(prompt + " [default value = " + dflt + "]  ");
                System.out.flush();

                int i=0;
                while (!done){
                        try{
                                ch = System.in.read();
                                if (ch < 0 || (char)ch == '\n' || (char)ch =='\r'){
                                        if(i==0){
                                            phstring = dflt;
                                            if((char)ch == '\r')ch = System.in.read();
                                        }
                                        done = true;
                                }
                                else{
                                        phstring = phstring + (char) ch;
                                        i++;
                                 }
                        }
                        catch(java.io.IOException e){
                                done = true;
                        }
                }
                return Phasor.parsePhasor(phstring);
        }

        // Read a Phasor from the keyboard console after a prompt message (with Phasor default option)
        // in a String format compatible with Phasor.parse,
        // 'magnitude'<'phase', 'magnitude'<'phase'deg, 'magnitude'<'phase'rad
        // e.g. 1.23<34.1deg, -0.67<-56.7, 6.8e2<-0.22rad
        // prompt = Prompt message to vdu
        // dflt = default value
        public static final synchronized Phasor readPhasor(String prompt, Phasor dflt)
        {
                int ch = ' ';
                String phstring = "";
                boolean done = false;

                System.out.print(prompt + " [default value = " + dflt + "]  ");
                System.out.flush();

                int i=0;
                while (!done){
                        try{
                                ch = System.in.read();
                                if (ch < 0 || (char)ch == '\n' || (char)ch =='\r'){
                                        if(i==0){
                                            if((char)ch == '\r')ch = System.in.read();
                                            return dflt;
                                        }
                                        done = true;
                                }
                                else{
                                        phstring = phstring + (char) ch;
                                        i++;
                                 }
                        }
                        catch(java.io.IOException e){
                                done = true;
                        }
                }
                return Phasor.parsePhasor(phstring);
        }

        // Read a Phasor from the keyboard console without a prompt message
        // in a String format compatible with Phasor.parse,
        // 'magnitude'<'phase', 'magnitude'<'phase'deg, 'magnitude'<'phase'rad
        // e.g. 1.23<34.1deg, -0.67<-56.7, 6.8e2<-0.22rad
        public static final synchronized Phasor readPhasor(){
                int ch = ' ';
                String phstring = "";
                boolean done = false;

                System.out.print(" ");
                System.out.flush();

                while (!done){
                        try{
                                ch = System.in.read();
                                if (ch < 0 || (char)ch == '\n')
                                        done = true;
                                else
                                        phstring = phstring + (char) ch;
                        }
                        catch(java.io.IOException e){
                                done = true;
                        }
                }
                return Phasor.parsePhasor(phstring);
        }


        // PRINT A PHASOR
        // Print to terminal window with text (message) and a line return
        public void println(String message){
                System.out.println(message + " " + this.toString());
        }

        // Print to terminal window without text (message) but with a line return
        public void println(){
                System.out.println(" " + this.toString());
        }

        // Print to terminal window with text (message) but without line return
        public void print(String message){
                System.out.print(message + " " + this.toString());
        }

        // Print to terminal window without text (message) and without line return
        public void print(){
                System.out.print(" " + this.toString());
        }

        // PRINT AN ARRAY OF PHASORS
        // Print an array to terminal window with text (message) and a line return
        public static void println(String message, Phasor[] aa){
            System.out.println(message);
            for(int i=0; i>>32));
                int hphase = (int)(lphase^(lphase>>>32));
                return 6*(hmagnt/10)+4*(hphase/10);
        }


        // ARRAYS

        // Create a one dimensional array of Phasors of length n
        // all magnitudes = 1 and all phases = 0
        public static Phasor[] oneDarray(int n){
                Phasor[] a = new Phasor[n];
                Phasor b = new Phasor();
                b.reset(1.0, 0.0);
                for(int i=0; imdn)mdn=Math.abs(ma);
                if(mdn==0.0D){
                    mtest=0.0;
                }
                else{
                    mtest=Math.abs(ma-mt)/mdn;
                }
                pdn=Math.abs(pt);
                if(Math.abs(pa)>pdn)pdn=Math.abs(pa);
                if(pdn==0.0D){
                    ptest=0.0;
                }
                else{
                    ptest=Math.abs(pa-pt)/pdn;
                }
                if(mtest 

Ads help maintain this website.