~ubuntu-branches/debian/sid/gsl/sid

« back to all changes in this revision

Viewing changes to vector/oper_complex_source.c

  • Committer: Bazaar Package Importer
  • Author(s): Dirk Eddelbuettel
  • Date: 2008-12-16 06:17:55 UTC
  • mfrom: (1.3.2 upstream) (3.1.15 jaunty)
  • Revision ID: james.westby@ubuntu.com-20081216061755-9la7p0qwrhopk8pl
Tags: 1.12+dfsg-1
* New upstream version released today

* doc/*: As before, removed the 'non-free' documentation to create a 
  source package that complies with Debian's interpretation of what is free. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* vector/oper_source.c
 
2
 * 
 
3
 * Copyright (C) 2008 Brian Gough
 
4
 * 
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 3 of the License, or (at
 
8
 * your option) any later version.
 
9
 * 
 
10
 * This program is distributed in the hope that it will be useful, but
 
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * General Public License for more details.
 
14
 * 
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
18
 */
 
19
 
 
20
int 
 
21
FUNCTION(gsl_vector, add) (TYPE(gsl_vector) * a, const TYPE(gsl_vector) * b)
 
22
{
 
23
  const size_t N = a->size;
 
24
 
 
25
  if (b->size != N)
 
26
    {
 
27
      GSL_ERROR ("vectors must have same length", GSL_EBADLEN);
 
28
    }
 
29
  else 
 
30
    {
 
31
      const size_t stride_a = a->stride;
 
32
      const size_t stride_b = b->stride;
 
33
 
 
34
      size_t i;
 
35
 
 
36
      for (i = 0; i < N; i++)
 
37
        {
 
38
          a->data[2 * i * stride_a] += b->data[2 * i * stride_b];
 
39
          a->data[2 * i * stride_a + 1] += b->data[2 * i * stride_b + 1];
 
40
        }
 
41
      
 
42
      return GSL_SUCCESS;
 
43
    }
 
44
}
 
45
 
 
46
int 
 
47
FUNCTION(gsl_vector, sub) (TYPE(gsl_vector) * a, const TYPE(gsl_vector) * b)
 
48
{
 
49
  const size_t N = a->size;
 
50
 
 
51
  if (b->size != N)
 
52
    {
 
53
      GSL_ERROR ("vectors must have same length", GSL_EBADLEN);
 
54
    }
 
55
  else 
 
56
    {
 
57
      const size_t stride_a = a->stride;
 
58
      const size_t stride_b = b->stride;
 
59
 
 
60
      size_t i;
 
61
 
 
62
      for (i = 0; i < N; i++)
 
63
        {
 
64
          a->data[2 * i * stride_a] -= b->data[2 * i * stride_b];
 
65
          a->data[2 * i * stride_a + 1] -= b->data[2 * i * stride_b + 1];
 
66
        }
 
67
      
 
68
      return GSL_SUCCESS;
 
69
    }
 
70
}
 
71
 
 
72
int 
 
73
FUNCTION(gsl_vector, mul) (TYPE(gsl_vector) * a, const TYPE(gsl_vector) * b)
 
74
{
 
75
  const size_t N = a->size;
 
76
 
 
77
  if (b->size != N)
 
78
    {
 
79
      GSL_ERROR ("vectors must have same length", GSL_EBADLEN);
 
80
    }
 
81
  else 
 
82
    {
 
83
      const size_t stride_a = a->stride;
 
84
      const size_t stride_b = b->stride;
 
85
 
 
86
      size_t i;
 
87
 
 
88
      for (i = 0; i < N; i++)
 
89
        {
 
90
          ATOMIC ar = a->data[2 * i * stride_a];
 
91
          ATOMIC ai = a->data[2 * i * stride_a + 1];
 
92
          
 
93
          ATOMIC br = b->data[2 * i * stride_b];
 
94
          ATOMIC bi = b->data[2 * i * stride_b + 1];
 
95
 
 
96
          a->data[2 * i * stride_a] = ar * br - ai * bi;
 
97
          a->data[2 * i * stride_a + 1] = ar * bi + ai * br;
 
98
        }
 
99
      
 
100
      return GSL_SUCCESS;
 
101
    }
 
102
}
 
103
 
 
104
int 
 
105
FUNCTION(gsl_vector, div) (TYPE(gsl_vector) * a, const TYPE(gsl_vector) * b)
 
106
{
 
107
  const size_t N = a->size;
 
108
 
 
109
  if (b->size != N)
 
110
    {
 
111
      GSL_ERROR ("vectors must have same length", GSL_EBADLEN);
 
112
    }
 
113
  else 
 
114
    {
 
115
      const size_t stride_a = a->stride;
 
116
      const size_t stride_b = b->stride;
 
117
 
 
118
      size_t i;
 
119
 
 
120
      for (i = 0; i < N; i++)
 
121
        {
 
122
          ATOMIC ar = a->data[2 * i * stride_a];
 
123
          ATOMIC ai = a->data[2 * i * stride_a + 1];
 
124
          
 
125
          ATOMIC br = b->data[2 * i * stride_b];
 
126
          ATOMIC bi = b->data[2 * i * stride_b + 1];
 
127
 
 
128
          ATOMIC s = 1.0 / hypot(br, bi);
 
129
          
 
130
          ATOMIC sbr = s * br;
 
131
          ATOMIC sbi = s * bi;
 
132
          
 
133
          a->data[2 * i * stride_a] = (ar * sbr + ai * sbi) * s;
 
134
          a->data[2 * i * stride_a + 1] = (ai * sbr - ar * sbi) * s;
 
135
        }
 
136
      
 
137
      return GSL_SUCCESS;
 
138
    }
 
139
}
 
140
 
 
141
int 
 
142
FUNCTION(gsl_vector, scale) (TYPE(gsl_vector) * a, const BASE x)
 
143
{
 
144
  const size_t N = a->size;
 
145
  const size_t stride = a->stride;
 
146
  
 
147
  size_t i;
 
148
  
 
149
  ATOMIC xr = GSL_REAL(x);
 
150
  ATOMIC xi = GSL_IMAG(x);
 
151
 
 
152
  for (i = 0; i < N; i++)
 
153
    {
 
154
      ATOMIC ar = a->data[2 * i * stride];
 
155
      ATOMIC ai = a->data[2 * i * stride + 1];
 
156
          
 
157
      a->data[2 * i * stride] = ar * xr - ai * xi;
 
158
      a->data[2 * i * stride + 1] = ar * xi + ai * xr;
 
159
    }
 
160
  
 
161
  return GSL_SUCCESS;
 
162
}
 
163
 
 
164
int 
 
165
FUNCTION(gsl_vector, add_constant) (TYPE(gsl_vector) * a, const BASE x)
 
166
{
 
167
  const size_t N = a->size;
 
168
  const size_t stride = a->stride;
 
169
  
 
170
  size_t i;
 
171
 
 
172
  ATOMIC xr = GSL_REAL(x);
 
173
  ATOMIC xi = GSL_IMAG(x);
 
174
  
 
175
  for (i = 0; i < N; i++)
 
176
    {
 
177
      a->data[2 * i * stride] += xr;
 
178
      a->data[2 * i * stride + 1] += xi;
 
179
    }
 
180
  
 
181
  return GSL_SUCCESS;
 
182
}