File: wncplx.c

package info (click to toggle)
libwn6 6.0-3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,996 kB
  • ctags: 3,938
  • sloc: ansic: 45,083; makefile: 926; csh: 274; sh: 12
file content (187 lines) | stat: -rw-r--r-- 3,260 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**********************************************************************

wn_cplx_make(pnumber)
wn_cplx_enter(c)
wn_cplx_print(c)

wn_cplx_add(result,c1,c2)
wn_cplx_subtract(result,c1,c2)
wn_cplx_multiply(result,c1,c2)
wn_cplx_divide(result,c1,c2)
wn_cplx_reciprical(result,c)

double wn_cplx_norm_squared(c)
double wn_cplx_norm(c)
wn_cplx_to_polar(pr,ptheta,c)
wn_polar_to_cplx(c,r,theta)

**********************************************************************/
/****************************************************************************

COPYRIGHT NOTICE:

  The source code in this file is provided free of charge
  to the author's consulting clients.  It is in the
  public domain and therefore may be used by anybody for
  any purpose.

AUTHOR:

  Will Naylor

****************************************************************************/

#include <stdio.h>
#include <math.h>

#include "wnlib.h"
#include "wnmem.h"
#include "wnsqr.h"
#include "wncplx.h"



void wn_cplx_make(wn_cplx *pnumber)
{
  *pnumber = (wn_cplx)wn_zalloc(sizeof(struct wn_cplx_struct));

  (*pnumber)->real = 0.0;
  (*pnumber)->imag = 0.0;
}


void wn_cplx_copy(wn_cplx out,wn_cplx in)
{
  out->real = in->real;
  out->imag = in->imag;
}


void wn_cplx_enter(wn_cplx c)
{
  double real,imag;

  scanf("%lf,%lf",&real,&imag);

  c->real = real;
  c->imag = imag;
}


void wn_cplx_print(wn_cplx c)
{
  printf("(%lf,%lf)",c->real,c->imag);
}


void wn_cplx_add(wn_cplx result,wn_cplx c1,wn_cplx c2)
{
  result->real = c1->real + c2->real;
  result->imag = c1->imag + c2->imag;
}


void wn_cplx_subtract(wn_cplx result,wn_cplx c1,wn_cplx c2)
{
  result->real = c1->real - c2->real;
  result->imag = c1->imag - c2->imag;
}


void wn_cplx_multiply(wn_cplx result,wn_cplx c1,wn_cplx c2)
{
  result->real = (c1->real*c2->real) - (c1->imag*c2->imag);
  result->imag = (c1->real*c2->imag) + (c1->imag*c2->real);
}


void wn_cplx_divide(wn_cplx result,wn_cplx c1,wn_cplx c2)
{
  struct wn_cplx_struct r2_struct;
  wn_cplx r2 = &r2_struct;

  wn_cplx_reciprical(r2,c2);
  wn_cplx_multiply(result,c1,r2);
}


void wn_cplx_reciprical(wn_cplx result,wn_cplx c)
{
  double norm_squared;

  norm_squared = wn_cplx_norm_squared(c);

  result->real = c->real / norm_squared;
  result->imag = -(c->imag / norm_squared);
}


double wn_cplx_norm_squared(wn_cplx c)
{
  return(wn_square(c->real) + wn_square(c->imag));
}


double wn_cplx_norm(wn_cplx c)
{
  return(sqrt(wn_cplx_norm_squared(c)));
}


void wn_cplx_to_polar(double *pr,double *ptheta,wn_cplx c)
{
  *pr = wn_cplx_norm(c);
  *ptheta = atan2(c->imag,c->real);
}


void wn_polar_to_cplx(wn_cplx c,double r,double theta)
{
  c->real = r*cos(theta);
  c->imag = r*sin(theta);
}


void wn_cplx_make_vect(wn_cplx **pvector,int len_i)
{
  int i;

  *pvector = (wn_cplx *)wn_zalloc(len_i*sizeof(wn_cplx));

  for(i=0;i<len_i;i++)
  {
    wn_cplx_make(&((*pvector)[i]));
  }
}


void wn_cplx_copy_vect(wn_cplx out[],wn_cplx in[],int len_i)
{
  int i;

  for(i=0;i<len_i;++i)
  {
    wn_cplx_copy(out[i],in[i]);
  }
}


void wn_cplx_conjugate_vect
(
  register wn_cplx *vector,
  int len_i
)
{
  register wn_cplx number,*fin;

  fin = vector+len_i;

  for(;vector != fin;++vector)
  {
    number = *vector;

    number->imag = -number->imag;
  }
}