Recent

Author Topic: [Fixed Thanks WP] Missing function in "ucomplex" "cabs"  (Read 1612 times)

jamie

  • Hero Member
  • *****
  • Posts: 7329
[Fixed Thanks WP] Missing function in "ucomplex" "cabs"
« on: October 11, 2025, 11:55:08 pm »
Trying to convert over some C code and from what I understand I can emulate this function, however, I would like it to interact with the ucomplex unit and if someone could spit out an equal to this function please.

 I am sure its just a ABS return but online references talk about  2+2*I etc

also, the "conj" function which I assume the cong function  or cmod function could be the same?

Jamie

« Last Edit: October 13, 2025, 08:41:20 pm by jamie »
The only true wisdom is knowing you know nothing

Paolo

  • Hero Member
  • *****
  • Posts: 639
Re: Missing function in "ucomplex" "cabs"
« Reply #1 on: October 12, 2025, 12:20:24 am »
Given a complex number z=a+ib
Cabs should be the modulus |z|=sqrt(a*a+b*b)
The complex conjugated of z is z*=a-ib

wp

  • Hero Member
  • *****
  • Posts: 13228
Re: Missing function in "ucomplex" "cabs"
« Reply #2 on: October 12, 2025, 12:24:25 am »
Consider a complex number like a 2d vector. The x component is named "real part", and the y component "imaginary part". Both are always displayed as a sum "(real part) + i*(imaginary part)" where "i" is the "imaginary unit".

The complex absolute value is the length of this "2D vector" --> sqrt(sqr(real part) + sqr(imaginary part)). In ucomplex, this is done by the function cmod().

The complex conjugate of a complex number is the number which has then same real part but negated imaginary part. In ucomplex, this is done by the function cong() (strange, why wasn't it named "conj"?).

[EDIT]
Paolo was faster...

jamie

  • Hero Member
  • *****
  • Posts: 7329
Re: Missing function in "ucomplex" "cabs"
« Reply #3 on: October 12, 2025, 12:43:17 am »
I am looking at C Omplex.h file online and looking at examples, they don't seem to generate the same values.

Something is strange in Demark as many say.
https://www.tutorialspoint.com/c_standard_library/c_function_cabs.htm

That for example.

Thanks for the heads up, I'll sort this out I guess.

Jamie
The only true wisdom is knowing you know nothing

wp

  • Hero Member
  • *****
  • Posts: 13228
Re: Missing function in "ucomplex" "cabs"
« Reply #4 on: October 12, 2025, 01:11:18 am »
The three samples are calculated correctly.
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. uses
  3.   ucomplex;
  4. var
  5.   z, z1, z2: complex;
  6. begin
  7.   WriteLn('Example 1');
  8.   z := CInit(3.5, 2.21);  // z = 3.5 + 2.21*i
  9.   WriteLn('|z| = |', cstr(z,0,3), '| = ', CMod(z):0:3);
  10.  
  11.   WriteLn;
  12.   WriteLn('Example 2');
  13.   z := CInit(5.0, 6.0);
  14.   WriteLn('The complex number is ', z.re:0:2, ' + ', CMod(z):0:2, 'i');
  15.   WriteLn('The magnitude of z is ', CMod(z):0:2);
  16.  
  17.   WriteLn;
  18.   WriteLn('Example 3');
  19.   z1 := CInit(3.0, 5.0);
  20.   z2 := CInit(4.0, 3.0);
  21.   z := z2 - z1;
  22.   WriteLn('z: ', z.re:0:1, ' + ', z.im:0:1, 'i');
  23.   WriteLn('Complex absolute value: ', CMod(z2 - z1):0:2);
  24.  
  25.   ReadLn;
  26. end.
Code: [Select]
Example 1
|z| = |3.500+2.210i| = 4.139

Example 2
The complex number is 5.00 + 7.81i
The magnitude of z is 7.81

Example 3
z: 1.0 + -2.0i
Complex absolute value: 2.24

Paolo

  • Hero Member
  • *****
  • Posts: 639
Re: Missing function in "ucomplex" "cabs"
« Reply #5 on: October 12, 2025, 01:20:37 am »
@wp, example 2 there is a typo, the result is correct, but you print the wrong input z, it is 5+i6, not 5+i7.81.

wp

  • Hero Member
  • *****
  • Posts: 13228
Re: Missing function in "ucomplex" "cabs"
« Reply #6 on: October 12, 2025, 01:25:15 am »
No, it is not a typo, I thought the same initially. But the output is not for z but for a new complex number derived from z having the abs as imaginary part. A strange, confusing example, I agree...

jamie

  • Hero Member
  • *****
  • Posts: 7329
Re: Missing function in "ucomplex" "cabs"
« Reply #7 on: October 12, 2025, 01:27:24 am »
The problem I am running into is "complex" in this c-lib is actually a Double  and it does not actually have a struct layout.

Quote
A complex number represented as x + yi, where x is the real part, y is the imaginary part, and i is the imaginary unit (defined as the square root of -1).


So it builds into a float.

Code: Pascal  [Select][+][-]
  1. double complex t = cexp(-2.0 * I * M_PI * k / n) * odd[k];
  2.  

so either the complex type is heavely operator overloaded or it is just a Double?

The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 7329
Re: Missing function in "ucomplex" "cabs"
« Reply #8 on: October 12, 2025, 01:39:28 am »
if anyone is interested in what I am trying here.

The only true wisdom is knowing you know nothing

Paolo

  • Hero Member
  • *****
  • Posts: 639
Re: Missing function in "ucomplex" "cabs"
« Reply #9 on: October 12, 2025, 01:44:03 am »
Uhmmmm, I did not see the original code, but in what you show at row 13 z is set as 5+i6, and its modulus is 7.81, that is printed correctly. But in the output just above  the printed modulus it is said that z=5+7.81i, and this cannot have modulus=7.81.  So input and output are not consistent.

Paolo

  • Hero Member
  • *****
  • Posts: 639
Re: Missing function in "ucomplex" "cabs"
« Reply #10 on: October 12, 2025, 01:49:25 am »
I'll check your attached file tomorrow. From what I see up to now, you are trying to calculate e^i2pik/n, this can be translate as complex number z=cos(2pik/n)+i*sin(2pik/n)

jamie

  • Hero Member
  • *****
  • Posts: 7329
Re: Missing function in "ucomplex" "cabs"
« Reply #11 on: October 12, 2025, 03:17:09 am »
I found the C header file here or at least a version of it.
https://gcc.gnu.org/onlinedocs/gcc-11.4.0/libstdc++/api/a00071_source.html

It is most definitely a Class with lots of operators.

I think I can convert this sample code over to the use of the UComplex.

Jamie
The only true wisdom is knowing you know nothing

Thausand

  • Sr. Member
  • ****
  • Posts: 405
Re: Missing function in "ucomplex" "cabs"
« Reply #12 on: October 12, 2025, 08:00:14 am »
I found the C header file here or at least a version of it.
https://gcc.gnu.org/onlinedocs/gcc-11.4.0/libstdc++/api/a00071_source.html
Can find many function and have search and explain https://en.cppreference.com/w/

Also have other complex unit, dmath, lmath and other https://wiki.freepascal.org/complex_number (have read see also)

Quote
It is most definitely a Class with lots of operators.
Can also simple not use operator (unit ucomplex have operators that need for example).

Quote
I think I can convert this sample code over to the use of the UComplex.
Have attach if want, attempt mine.
« Last Edit: October 12, 2025, 08:04:30 am by Thausand »

jamie

  • Hero Member
  • *****
  • Posts: 7329
Re: Missing function in "ucomplex" "cabs"
« Reply #13 on: October 12, 2025, 09:50:53 pm »
Can someone please check my work to see if the conversion looks, correct?

I have the original C and mine here.
thanks.

Jamie


The only true wisdom is knowing you know nothing

Thausand

  • Sr. Member
  • ****
  • Posts: 405
Re: Missing function in "ucomplex" "cabs"
« Reply #14 on: October 13, 2025, 01:02:14 am »
Can someone please check my work to see if the conversion looks, correct?
Look is deception   :)

BTW look is for me ok (not have test and I no expert) and can test self better if can compile c program.

- Compile program c
- run program c, make pipe output have file "two.txt".
- make modify main for c.
Code: C  [Select][+][-]
  1. int main() {
  2.     // Example signal (sine wave + noise)
  3.     double complex signal[N];
  4.     for (int i = 0; i < N; i++) {
  5.         signal[i] = sin(2 * M_PI * i / N) + ((rand() % 100) / 100.0 - 0.5);  // Sine wave + noise
  6.     }
  7.  
  8.     // Perform FFT
  9. //    fft(signal, N);
  10.  
  11.     // Remove noise
  12. //    remove_noise(signal, N, THRESHOLD);
  13.  
  14.     // Perform inverse FFT
  15. //    ifft(signal, N);
  16.  
  17.     // Output the cleaned signal
  18.     for (int i = 0; i < N; i++) {
  19.         printf("%f\n", creal(signal[i]));
  20.     }
  21.  
  22.     return 0;
  23. }
  24.  

- Compile modify program c
- run modify program c, make pipe output have file "one.txt".
- make modify program pascal
Code: Pascal  [Select][+][-]
  1. ...
  2. // new procedure
  3. procedure testusefile(Filename: string; var values: TComplexArray; n: integer);
  4. var
  5.   i: integer;
  6.   List:TStringList;
  7. begin
  8.   List := TStringList.Create;
  9.   List.LoadFromFile(Filename);
  10.  
  11.   for i := 0 to NUM - 1 do
  12.     values[i].re := List[i].ToSingle;
  13.  
  14.   List.Free;
  15. end;
  16.  
  17. Var
  18.    signal:Array[0..N-1] of Complex;//Example signal;
  19.    i:integer;
  20. Begin
  21.    //Create a signal with random noise added.
  22.  
  23. // ++ make comment
  24. // For i := 0 to n-1 do
  25. //    signal[i] := sin(2 * 6.28 * i / n)+((Random mod 100) / 100.0-0.5);
  26.  
  27. // ++ new procedure
  28.     testusefile('one.txt', signal, NUM);
  29.  
  30.  
  31.     // Perform FFT
  32.     fft(signal, N);
  33.  
  34.     // Remove noise
  35.     remove_noise(signal, N, THRESHOLD);
  36.  
  37.     // Perform inverse FFT
  38.     ifft(signal, N);
  39.  
  40.     // Output the cleaned signal
  41.     For i := 0 to n-1 do
  42.      Writeln(cReal(Signal[i]));
  43. End.
  44.  

- Compile modify program pascal
- run modify program pascal, make pipe output have file "pascal.txt".

Then have compare output two.txt <-> pascal.txt

If want can make every calculate step and compare (4 total) and see where calculate ok or calculate wrong.

Compare value is no 100% same and can have error round very right side digit.

If not can compile c then write and I can make value for 4 step and post and you can use for test.

 

TinyPortal © 2005-2018