Difference between revisions of "complex number"

From Lazarus wiki
Jump to navigationJump to search
m (Fixed template loop)
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{LanguageBar}}
+
{{complex_number}}
  
 
Complex numbers is a mathematical concept providing solutions to equations such as <math>x^2 = -1</math>.
 
Complex numbers is a mathematical concept providing solutions to equations such as <math>x^2 = -1</math>.
In [[FPC]]'s default [[RTL|runtime library]] the unit <syntaxhighlight lang="pascal" enclose="none">uComplex</syntaxhighlight> defines a type <syntaxhighlight lang="pascal" enclose="none">complex</syntaxhighlight> and lots of operator and other functions.
+
In [[FPC]]'s default [[RTL|runtime library]] the unit <syntaxhighlight lang="pascal" inline>uComplex</syntaxhighlight> defines a type <syntaxhighlight lang="pascal" inline>complex</syntaxhighlight> and lots of operator and other functions.
<syntaxhighlight lang="pascal" enclose="none">u</syntaxhighlight> in <syntaxhighlight lang="pascal" enclose="none">uComplex</syntaxhighlight> stands for the Greek letter μ, meaning “micro”, as the implementation is kept as simple as possible.
+
<syntaxhighlight lang="pascal" inline>u</syntaxhighlight> in <syntaxhighlight lang="pascal" inline>uComplex</syntaxhighlight> stands for the Greek letter μ, meaning “micro”, as the implementation is kept as simple as possible.
  
In [[Extended Pascal#Complex Numbers|extended Pascal]], which FPC plans to implement one day, the data type <syntaxhighlight lang="pascal" enclose="none">complex</syntaxhighlight> is defined as part of the language.
+
In [[Extended Pascal#Complex Numbers|Extended Pascal]], which FPC plans to implement one day, the data type <syntaxhighlight lang="pascal" inline>complex</syntaxhighlight> is defined as part of the language.
  
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
program complexDemo(input, output, stderr);
+
program ComplexDemo;
 
uses
 
uses
 
uComplex;
 
uComplex;
Line 26: Line 26:
 
writeLn('x = ', x.re, ' + ', x.im, 'i');
 
writeLn('x = ', x.re, ' + ', x.im, 'i');
 
end.
 
end.
 +
</syntaxhighlight>
 +
 +
Another demo, which shows how to use functions <syntaxhighlight lang="pascal" inline>cinit</syntaxhighlight> (assign value), <syntaxhighlight lang="pascal" inline>cstr</syntaxhighlight> (convert value to string), <syntaxhighlight lang="pascal" inline>csin</syntaxhighlight> (sinus):
 +
<syntaxhighlight lang="pascal">
 +
program ComplexDemo2;
 +
uses
 +
  uComplex;
 +
const
 +
  L = 0;
 +
  D = 3;
 +
var
 +
  c1, c2: complex;
 +
begin
 +
  c1 := cinit(1.0, 1.0);
 +
  c2 := cinit(2.0, -2.0);
 +
  WriteLn('c1      = ' + cstr(c1, L, D));
 +
  WriteLn('c2      = ' + cstr(c2, L, D));
 +
  WriteLn('c1+c2  = ' + cstr(c1+c2, L, D));
 +
  WriteLn('c1-c2  = ' + cstr(c1-c2, L, D));
 +
  WriteLn('c1*c2  = ' + cstr(c1*c2, L, D));
 +
  WriteLn('c1/c2  = ' + cstr(c1/c2, L, D));
 +
  WriteLn('sin(c1) = ' + cstr(csin(c1), L, D));
 +
  ReadLn;
 +
end.
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
== see also ==
 
== see also ==
* [[DMath]], a mathematical library also containing a complex number implementation
+
* [[DMath]], a mathematical library also containing a complex number implementation.
 
* [[LMath]], further development of LMath library, with completer support for complex numbers, where operators over them are defined.
 
* [[LMath]], further development of LMath library, with completer support for complex numbers, where operators over them are defined.
* [[NumLib Documentation#Complex_numbers|NumLib documentation]], where <syntaxhighlight lang="pascal" enclose="none">typ.complex</syntaxhighlight> is an [[Object|<syntaxhighlight lang="pascal" enclose="none">object</syntaxhighlight>]]
+
* [[NumLib Documentation#Complex_numbers|NumLib documentation]], where <syntaxhighlight lang="pascal" inline>typ.complex</syntaxhighlight> is an [[Object|<syntaxhighlight lang="pascal" inline>object</syntaxhighlight>]].
  
 
== external references ==
 
== external references ==
 
* [https://en.wikipedia.org/wiki/Complex_number “complex number” on Wikipedia, the free online encyclopedia]
 
* [https://en.wikipedia.org/wiki/Complex_number “complex number” on Wikipedia, the free online encyclopedia]
* [https://svn.freepascal.org/cgi-bin/viewvc.cgi/tags/release_3_0_4/packages/rtl-extra/src/inc/ucomplex.pp?view=markup <tt>packages/rtl-extra/src/inc/ucomplex.pp</tt> source code]
+
* [https://gitlab.com/freepascal.org/fpc/source/-/blob/main/packages/rtl-extra/src/inc/ucomplex.pp <tt>packages/rtl-extra/src/inc/ucomplex.pp</tt> source code]
* [https://svn.freepascal.org/cgi-bin/viewvc.cgi/tags/release_3_0_4/packages/numlib/src/typ.pas?view=markup#l134 <tt>packages/numlib/src/typ.pas</tt> source code]
+
* [https://gitlab.com/freepascal.org/fpc/source/-/blob/main/packages/numlib/src/typ.pas <tt>packages/numlib/src/typ.pas</tt> source code]
 
 
[[Category:Mathematics]]
 
[[Category:Code]]
 

Latest revision as of 17:14, 6 August 2022

English (en) français (fr)

Complex numbers is a mathematical concept providing solutions to equations such as [math]\displaystyle{ x^2 = -1 }[/math]. In FPC's default runtime library the unit uComplex defines a type complex and lots of operator and other functions. u in uComplex stands for the Greek letter μ, meaning “micro”, as the implementation is kept as simple as possible.

In Extended Pascal, which FPC plans to implement one day, the data type complex is defined as part of the language.

program ComplexDemo;
uses
	uComplex;
var
	x, y: complex;
begin
	// specifying real and imaginary part
	x := -5 + 2 * i;
	
	// specifying magnitude and phase angle
	// y := sqrt(2) * (cos(pi/4) + i * sin(pi/4))
	y.re :=  1;
	y.im :=  1;
	
	x := x + y;
	// there is no toString functionality:
	writeLn('x = ', x.re, ' + ', x.im, 'i');
end.

Another demo, which shows how to use functions cinit (assign value), cstr (convert value to string), csin (sinus):

program ComplexDemo2;
uses
  uComplex;
const
  L = 0;
  D = 3;
var
  c1, c2: complex;
begin
  c1 := cinit(1.0, 1.0);
  c2 := cinit(2.0, -2.0);
  WriteLn('c1      = ' + cstr(c1, L, D));
  WriteLn('c2      = ' + cstr(c2, L, D));
  WriteLn('c1+c2   = ' + cstr(c1+c2, L, D));
  WriteLn('c1-c2   = ' + cstr(c1-c2, L, D));
  WriteLn('c1*c2   = ' + cstr(c1*c2, L, D));
  WriteLn('c1/c2   = ' + cstr(c1/c2, L, D));
  WriteLn('sin(c1) = ' + cstr(csin(c1), L, D));
  ReadLn;
end.

see also

  • DMath, a mathematical library also containing a complex number implementation.
  • LMath, further development of LMath library, with completer support for complex numbers, where operators over them are defined.
  • NumLib documentation, where typ.complex is an object.

external references