Complex Numbers problem

I am having problems getting this Complex Numbers program to work correctly. i want to display using format a+bi the addition, subtraction and multiplication of 2 complex numbers.Can anyone reply today?
class Complex
     private double realPart, imaginaryPart;
     public Complex()
          realPart = 0.0;
          imaginaryPart = 0.0;
     public static void main(String [] args)
          Complex c;
          double res;
          c = new Complex();
          Screen.message("-------------------MENU----------------------------");
          Screen.newline(2);
          Screen.message("Please enter a number: ");
          Keyboard.readDouble();
          res = c.get_realPart();
          Screen.newline();
          Screen.message("Please enter a second number: ");
          Keyboard.readDouble();
          res = c.get_imaginaryPart();
          Screen.newline(2);
          Screen.message("--------------------------------------------------");
          Screen.newline();
          Screen.message("     Designed by Niall Magner               ");
          Screen.newline();
          Screen.message("--------------------------------------------------");
          Screen.newline(2);
          Screen.writedouble(res);
          Screen.newline();
          Screen.writedouble(res);
          Screen.newline();
          Screen.writedouble(res);
          Screen.newline(2);
     public double get_realPart()
          return realPart;     
     public double get_imaginaryPart()
          return imaginaryPart;
     public void addComplexNum(Complex n1, Complex n2)
          realPart = n1.get_realPart() + n2.get_realPart();
          imaginaryPart = n1.get_imaginaryPart() + n2.get_imaginaryPart();
          Screen.message("-----------------------------------------------");
          Screen.newline(2);
          Screen.message("Number 1 is: " + realPart);
          Screen.message(" + " + imaginaryPart + "i");
          Screen.newline();          
          Screen.message("Number 2 is: " + realPart);
          Screen.message(" + " + imaginaryPart + "i");
          Screen.newline();          
          Screen.message("-----------------------------------------------");
          Screen.newline(2);
     public void subtractComplexNum(Complex n1, Complex n2)
          realPart = n1.get_realPart() - n2.get_realPart();
          imaginaryPart = n1.get_imaginaryPart() - n2.get_imaginaryPart();
          Screen.message("-----------------------------------------------");
          Screen.newline(2);
          Screen.message("Number 1 is: " + realPart);
          Screen.message(" - " + imaginaryPart + "i");
          Screen.newline();
          Screen.message("Number 2 is: " + realPart);
          Screen.message(" - " + imaginaryPart + "i");
          Screen.newline();          
          Screen.message("-----------------------------------------------");
          Screen.newline(2);
     public void multiplyComplexNum(Complex n1, Complex n2)
          realPart = n1.get_realPart() * n2.get_realPart();
          imaginaryPart = n1.get_imaginaryPart() * n2.get_imaginaryPart();
          Screen.message("-----------------------------------------------");
          Screen.newline(2);
          Screen.message("Number 1 is: " + realPart);
          Screen.message(" * " + imaginaryPart + "i");
          Screen.newline();          
          Screen.message("Number 2 is: " + realPart);
          Screen.message(" * " + imaginaryPart + "i");
          Screen.newline();
          Screen.message("-----------------------------------------------");
          Screen.newline(2);
Any help greatly appreciated.
Niall Magner

There are several problems with this code. You never call the add method, so the numbers are never going to be added. You never load up a complex number to add either.
Try doing something a bit more like this:
  read a float from the keyboard
  read another float from the keyboard
  Create a complex number from this float
  Complex complex1 = new Complex();
  complex1.realPart = first number from above;
  complex1.imaginaryPart = second number from above;
  do this again to get the second complex number
  then add the numbers together
  Complex complexSum = new Complex
  complexSum.addComplexNum(complex1, complex2)
  print complexSum.realPart + "+i" + complexSum.imaginaryPart;
  then do the other operations you wantI would also advise that you take a course in Java Programming, cos the code you have posted is truely awful.

Similar Messages

  • SVD with complex numbers

    I'm working on creating a SVD class using complex numbers. I have an adaptation of the Jama ( http://math.nist.gov/javanumerics/jama/ ) implementation coded which works great with complex numbers with only a real part, but fails when the imaginary is present. I also coded up a port of numerical recipes' ( http://www.nr.com ) implementation, but am unable to get it to converge. I'm 99.9% sure that the problem does not exist in my complex number class as it has worked fine up until now.
    Anyhow, what I'm wondering is if anyone has a Java SVD for complex matrices that I could look at and/or use?
    Thanks in advance,
    m

    It wasn't working when I tried it a few months ago. I found the bug in the database - it's been long-standing. Given that Sun doesn't appear to be supporting Java3D very well, I doubt it'll ever be fixed.
    Besides, that SVD algorithm is only for real matricies, I believe. - MOD

  • Having trouble converting array to spreadsheet string, storing the file and coverting back to array with complex numbers

    I am working with a network analyzer. I have arrays made of 5 columns the first consisting of an integer and the next four consisting of complex numbers. I am converting the array into a spreadsheet string and then saving the file using the write characters to a file VI. That seems to work well as when I open the file in Excel all the data is there. However when I try to reverse the process, open file and convert back to array, I loose some of the data. Specifically the imaginary parts of my complex numbers are all going to zero. I have narrowed down the problem to be in the conversion from spreadsheet string to array and vice versa. I
    think the problem may be with the 'format' input to the VI. I do not have an adequate resource for this so I am not sure what to put in to accomplish my task. Any takers?

    Hi Biz
    I don't think there is a direct way of converting a complex number to a
    string, so when you convert the array to a spreadsheet string, the
    numbers would be converted to real data.
    However, you could try separating the real and imaginary parts using the
    "Numeric: Complex to Re/Im" function, and then store these - either in
    separate files or in adjacent columns/rows in the same file. Then, when
    you read in the data again, use the "Numeric: Re/Im to Complex" function
    to put the two "halves" together.
    If you actually want Excel to interpret the numbers as imaginary, then
    you'll probably want to create a string for each complex number of the
    form "Re + Im*i" (after separating the Re and Im parts), by using
    "String:Format into String" with 2 numeric inputs and the format string
    "%f+%fi".
    Reading the data back into Labview then would require splitting the
    string into the 2 pieces by using "Stringcan from String" with 2
    numeric outputs (smae precision as original numbers specified by the 2
    Default Value inputs) and the same format string "%f+%fi", and then using
    the above-mentioned "Numeric: Re/Im to Complex" function. It worked for
    me, so if you can't follow what I am describing, send me an email and I
    can email you what I did (LV 5.1.1).
    Paul
    Biz wrote:
    > Having trouble converting array to spreadsheet string, storing the
    > file and coverting back to array with complex numbers
    >
    > I am working with a network analyzer. I have arrays made of 5 columns
    > the first consisting of an integer and the next four consisting of
    > complex numbers. I am converting the array into a spreadsheet string
    > and then saving the file using the write characters to a file VI. That
    > seems to work well as when I open the file in Excel all the data is
    > there. However when I try to reverse the process, open file and
    > convert back to array, I loose some of the data. Specifically the
    > imaginary parts of my complex numbers are all going to zero. I have
    > narrowed down the problem to be in the conversion from spreadsheet
    > string to array and vice versa. I think the problem may be with the
    > 'format' input to the VI. I do not have an adequate resource for this
    > so I am not sure what to put in to accomplish my task. Any takers?
    Research Assistant
    School of Physiotherapy, Curtin University of Technology
    Selby Street, Shenton Park, Western Australia, Australia. 6008
    email: [email protected]
    Tel. +61 8 9266 4657 Fax. +61 8 9266 3699
    "Everyone who calls on the name of the Lord will be saved." Romans 10:12
    "For all have sinned and fall short of the glory of God, and are
    justified freely by his grace through the redemption that came by Christ
    Jesus." Romans 3:23-4

  • Waveform and Complex Numbers

    Great; in LV 7.0 Complex Numbers were included as an allowed subtype of Waveform data.
    Bad; the vis supporting Complex numbers in Waveforms have a large number of errors. Many Vis does not handle complex numbers and therefore strip off the complex part at the output of the vi. I spent a lot of time to discover and correct the errors in the vis that I used.
    Are NI or anybody else working on a patch for these VIs (part of vi.lib)?

    You can let R&D directly know of these problems by going to www.ni.com >> contact NI >> feedback (bottom left). These reports go directly to the R&D managers. You can also contact tech support and have them file a report of the bad behavior. Be sure to provide example program and specific information.

  • Complex Numbers: better implementation

    I've already created a Complex class, with all the mathematical methods necessary. But I've seen at the Internet several attempts to do such classes, and one preferred to make a compiler that accepts complex as a primitive type. And he's right. Creating a class to represent the complex numbers takes too much memory and time only to store two doubles. Using C/C++, complex numbers could be made with structs, and a Complex class would be used to implement the necessary methods, wrapping the complex type, like Double wraps a double.
    So, I'd like to suggest a new type of class. This class would act like
    an C/C++ struct, being able to store only fields and constructors. I believe that this way would make it waste less memory, and enable faster use of this class. It would work like this:
    public value class complex {
      public double real;
      public double imag;
    public class Complex {
      public static final complex I;
      I.real = 0; I.imag = 1;
    }What do you guys think?

    Creating a class to represent the complex
    numbers takes too much memory and time only to store two doubles.If the only fields in the Complex object are two doubles, how much more memory does a Complex object take up? The methods would only appear in the Class object, not each Complex instance. Right?
    I guess you have one reference on the stack and two doubles on the heap, as opposed to two doubles on the stack and nothing in the heap.
    So, I'd like to suggest a new type of class. This
    class would act like an C/C++ struct, being able to store only
    fields and constructors. I believe that this way would
    make it waste less memory, and enable faster use of this class.At this point I wonder whether there'd really be a big memory savings, and whether it would be worth watering down the OOP-ness of the language.
    Defining a complex primitive is another issue.
    Issues like ordering and equality would seem to make the problem really...complex... Perhaps any choice for those issues would only satisfy a third of the interested parties, and any design would satisfy no one.

  • How to use RLS with complex numbers as the input?

    I import a file which have complex numbers, and I want to filter it using complex RLS adaptive filter, but it always freeze at the filtering and update then it returns one value in the matrix and all the other numbers are NaN, so how can I fix this.
    The second thing is when I use FBLMS, it always return error 42020 unless I specify the filter length= number of the input which is huge number, so how do I specify normal filter length.

    Hi engomar,
    There is a detailed description of how to choose filter length in the Adaptive Filter Tollkit Help file. 
    http://zone.ni.com/reference/en-XX/help/372357B-01/lvaftconcepts/aft_choose_filterlength/
    This page has several links to other important factors of what filter to choose. Under Computational Resource Requirements, it lists the memory usage for different filter types. RLS is the most memory intensive, which could be causing the freeze. Maybe try a LMS to see if this helps.
    Robert R. | Applications Engineer | National Instruments

  • In range and coerce with complex numbers?

    I have an array of complex numbers with these numbers I need to set to 0 all numbers whose real parts are negative. With "in range and coerce" I can split the array into real and imaginary then set all negative numbers to 0 then recombine the complex part but that still leaves me with 0+ai numbers that should be 0. How can I also set the complex part to zero for the negative numbers?

    Hello,
    You can use the following fonction in a for loop with your array.
    Michel

  • Using complex numbers in Fletcher Reeves optimization method

    Hello,
    I would like to minimize a function of 4 complex variables with the Fletcher Reeves optimization method (Conjugate Gradient VI).
    Is it possible to do it with this VI or with HiQ4.5 ?
    (for S-parameters specialists) The purpose is to de-embed using optimization method.
    Thanks,
    Dze

    I believe that the "Conjugate Gradient nD.vi" and the optimize function in HiQ only work with real vectors. Most of the solve functions in HiQ work with complex numbers, and you may be able to create your own algorithm for the purpose, but I don't think that there is already a function that will do quite what you want.

  • Hp 35s integrating with complex numbers/functions

    Are there any examples of how to integrate a complex function for
    the HP 35s..the manual suggests its possible..but, I can not find one
    example...anywhere... I am just interested in testing.....I do
    not want to break things into parts etc...I just want to try
    e^z for example, where z is complex..any help?
    I have tried xiy, rTheta, x+iy...syntax errors or non existent..funny

    Hi,
    The manual states that complex numbers can be used with EXP(x), but it does not mention anything about using complex numbers for integration. Edit: I missed some text in the manual, see below.
    As far as I can tell it cannot do integration with complex numbers. (The 50g can do this.)
    However, you may get a more definitive answer if you ask your question here:
    http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/forum.cgi
    EDIT: that forum has moved to a new version:
    http://www.hpmuseum.org/forum/forum-4.html
    Note: I do not work for HP, I just like playing with calculators :-)

  • How do I write complex numbers to an excel or text spreadsheet file.

    I am performing an FFT on 3 channels of simultaneous analog input. I want to write the resultant 3 complex numbered arrays to a spreadsheet. Do I have to seperate the a and b*i components into r and theta and write two columns per array or is there a better way?

    Perhaps you can write it as a String..?
    Khalid

  • Need help with complex numbers ..

    Hi!
    Just want to check if i got it right - if anyone could pls confirm:
    public void divComplex(Complex a, Complex b)
    x = ((a.x * b.x) + (a.y * b.y))/((b.x * b.x) + (b.y * b.y));
    y = ((a.y * b.x) + (a.x * b.x))/((b.x * b.x) + (b.y * b.y));
    public void multComplex(Complex a, Complex b)
    x = ((a.x * b.x) - (a.y * b.y));
    y = ((a.x * b.y) + (a.y * b.x));
    where:
    protected double x;
    protected double y;
    is declared to represent the complex number .
    Is this correct?
    Siw

    For the division you have,
    a/b = a*b'/b*b' = a*b'/|b|
    |b| = (b.x + ib.y)*(b.x - ib.y) = b.x*b.x + b.y*b.y
    a*b' = (a.x + ia.y)*(b.x - ib.y) =
    (a.x*b.x + a.y*b.y) + i(a.y*b.x - a.x*b.y)
    There seems to be a slight mistake in the imaginary part (y).
    y = a.y*b.x - a.x*b.y / (b.x*b.x + b.y*b.y);
    But don't take my word for it. It's been ages since I did complex numbers -:)

  • How to convert a waveform signal into array of complex numbers

    How to convert a waveform signal into array of complex numbers.

    Hi Chaks,
    try this:
    (Get Waveform components, convert to complex DBL)
    Message Edited by GerdW on 01-28-2008 09:23 AM
    Best regards,
    GerdW
    CLAD, using 2009SP1 + LV2011SP1 + LV2014SP1 on WinXP+Win7+cRIO
    Kudos are welcome
    Attachments:
    ToCDB.png ‏1 KB

  • HP35s complex numbers

    My background is with the HP41C,CV,CX (41). I am now attempting to emulate routines I developed for the 41 for solutions on an HP35s (35) in RPN mode. My issue is with polar to rectangular conversion on the 35. The 41 had P-R and R-P keys. The 35 does not and wants to handle the conversion as “complex numbers”.The 41 key sequence is:RENTϴ (value)P-R X appears in x (stack) registerY appears in y (stack) registerThe stack contains two discrete values, x, y The 35 sequence is:Display .0Rϴ (key)ϴ (value)ENTx and y appear as x i y in the x registerCan I segregate x and y to complete the next 41 step? The next step in the 41 routine is Σ+.That operation generates cumulative Cartesian coordinates for each subsequent entry of R, ϴ in the Σx and Σy statistic registers Can that be emulated in the 35s?

    My background is with the HP41C,CV,CX (41). I am now attempting to emulate routines I developed for the 41 for solutions on an HP35s (35) in RPN mode. My issue is with polar to rectangular conversion on the 35. The 41 had P-R and R-P keys. The 35 does not and wants to handle the conversion as “complex numbers”.The 41 key sequence is:RENTϴ (value)P-R X appears in x (stack) registerY appears in y (stack) registerThe stack contains two discrete values, x, y The 35 sequence is:Display .0Rϴ (key)ϴ (value)ENTx and y appear as x i y in the x registerCan I segregate x and y to complete the next 41 step? The next step in the 41 routine is Σ+.That operation generates cumulative Cartesian coordinates for each subsequent entry of R, ϴ in the Σx and Σy statistic registers Can that be emulated in the 35s?

  • Tdms does not support complex numbers?

    I can see that I can create and manipulate complex waveforms in Labview, but when I try to save such a waveform in a TDMS file, I get a run-time error that data format is not supported, forcing me to split the complex waveform into two separate waveforms for saving.
    Also, when I send a complex waveform to a Waveform Graph indicator, it truncates the imaginary part.
    Are there any plans to add native support for complex numbers in TDMS and graphing?

    Fedor wrote:
    I can see that I can create and manipulate complex waveforms in Labview, but when I try to save such a waveform in a TDMS file, I get a run-time error that data format is not supported, forcing me to split the complex waveform into two separate waveforms for saving.
    That's correct. TDMS does not support complex values.
    Also, when I send a complex waveform to a Waveform Graph indicator, it truncates the imaginary part.
    That's also correct. Think about it. What would you plot on a waveform graph? You could use something like an XY graph, with X being the real values and Y being the imaginary values. Or, you could use a polar plot (available as a picture indicator plot).
    Are there any plans to add native support for complex numbers in TDMS and graphing?
    If you wish to make a suggestion regarding this, then you can post in the LabVIEW Idea Exchange, where people can vote on the idea. You may need to submit this as two different ideas since it's covering two different components.

  • 3-Phase Voltage Waveform to Complex Numbers

    I read this topic and and I do not think it worksfor my case. http://forums.ni.com/t5/LabVIEW/How-to-convert-waveform-into-complex-number/m-p/808360/highlight/true#M368430
    I would like to simulate 3-Phase voltage Waveform at the sample rate of 1000 sample/s and get their numberic values as complex numbers. I could only get the Y-value but not the phase.
    Could someone points out how to get the complex values (1000 values) in the attched example for VA, VB, and VC?
     

    Hi muahang1234,
    You could try something like the image I've attached. Basically, you generate the waveform and create another waveform that is 90 degrees out of phase. That way, you can track the Y position of each of the waveforms. The first waveform will be the real position while the other waveform's Y will be the imaginary position, or phase, of the waveform. You may have to convert this into polar form if you want it in that format, but that shouldn't be too hard.
    It may be a little inconvenient to do this for 3 waveforms, but that should at least give you an idea of what to do about the issue.
    Regards,
    Andy F.
    Applications Engineering
    National Instruments

Maybe you are looking for

  • User defined error handling in PLSQL procedure of portal form

    I need some help of building a user defined exception handling in PLSQL. I build a portal form based on a PLSQL procedure. In this procedure there are several SQL statements with exception handling. In case of exception I want to display my on error

  • Updating my CS6

    I am trying to update my CS6 but I get an error message # U44M1I208, How can I fix this?

  • Can one automatically remove exact duplicates?

    Using Itunes 10.2.2.12 on Windows XP I have inadvertantly somewhere dupkicated most of the music in my library Over 7k  items appear in the "displaying duplicates" Is there a way to get rid of them quickl;y without selecting each item individually?

  • Can anybody send me ebook for 'oracle 10g programming with pl/sql'

    Hi can any one send me ebook for oracle's 'oracle 10g programming with pl/sql', 'java' My mail id is: [email protected] Thanking in advance.

  • Can't play mp3 with sonata?

    Hi, Just installed Sonata om ny new fresh Arch installation. Alsa is configured and I can play the sound test aplay ....wav files... But now when im in X I started sonata and I cant ge the application to play my mp3 files? I can't even see them? And