Class: Linalg::DComplex

Inherits:
Object
  • Object
show all
Defined in:
lib/linalg/dcomplex.rb,
ext/linalg/dcomplex.c

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



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
# File 'ext/linalg/dcomplex.c', line 88

VALUE rb_dcomplex_initialize(int argc, VALUE* argv, VALUE self)
{
    VALUE rre ;
    VALUE rim ;
    doublecomplex* a ;
    int n = rb_scan_args(argc, argv, "02", &rre, &rim) ;
    get_doublecomplex(a, self) ;

    if( n == 2 )
    {
        a->r = (doublereal)NUM2DBL(rre) ;
        a->i = (doublereal)NUM2DBL(rim) ;
    }
    else if( n == 1 )
    {
        a->r = (doublereal)NUM2DBL(rre) ;
        a->i = (doublereal)0.0 ;
    }
    else
    {
        a->r = (doublereal)0.0 ;
        a->i = (doublereal)0.0 ;
    }
    
    return self ;
}

Instance Method Details

#*(rb) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'ext/linalg/dcomplex.c', line 183

VALUE rb_dcomplex_mul(VALUE self, VALUE rb)
{
    doublecomplex* a ;
    doublecomplex* c ;
    VALUE rc ;
    
    get_doublecomplex(a, self) ;
    c = ALLOC(doublecomplex) ;
    rc = wrap_doublecomplex(c) ;

    if( rb_obj_is_kind_of(rb, rb_cDComplex) )
    {
        doublecomplex* b ;
        get_doublecomplex(b, rb) ;
        c->r = a->r*b->r - a->i*b->i ;
        c->i = a->r*b->i + a->i*b->r ;
    }
    else
    {
        doublereal b = (doublereal)NUM2DBL(rb) ;
        c->r = a->r*b ;
        c->i = a->i*b ;
    }
    
    return rc ;
}

#+(rb) ⇒ Object



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
# File 'ext/linalg/dcomplex.c', line 129

VALUE rb_dcomplex_add(VALUE self, VALUE rb)
{
    doublecomplex* a ;
    doublecomplex* c ;
    VALUE rc ;
    
    get_doublecomplex(a, self) ;
    c = ALLOC(doublecomplex) ;
    rc = wrap_doublecomplex(c) ;

    if( rb_obj_is_kind_of(rb, rb_cDComplex) )
    {
        doublecomplex* b ;
        get_doublecomplex(b, rb) ;
        c->r = a->r + b->r ;
        c->i = a->i + b->i ;
    }
    else
    {
        doublereal b = (doublereal)NUM2DBL(rb) ;
        c->r = a->r + b ;
        c->i = a->i ;
    }
    
    return rc ;
}

#-(rb) ⇒ Object



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
# File 'ext/linalg/dcomplex.c', line 156

VALUE rb_dcomplex_sub(VALUE self, VALUE rb)
{
    doublecomplex* a ;
    doublecomplex* c ;
    VALUE rc ;
    
    get_doublecomplex(a, self) ;
    c = ALLOC(doublecomplex) ;
    rc = wrap_doublecomplex(c) ;

    if( rb_obj_is_kind_of(rb, rb_cDComplex) )
    {
        doublecomplex* b ;
        get_doublecomplex(b, rb) ;
        c->r = a->r - b->r ;
        c->i = a->i - b->i ;
    }
    else
    {
        doublereal b = (doublereal)NUM2DBL(rb) ;
        c->r = a->r - b ;
        c->i = a->i ;
    }
    
    return rc ;
}

#/(rb) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'ext/linalg/dcomplex.c', line 210

VALUE rb_dcomplex_div(VALUE self, VALUE rb)
{
    doublecomplex* a ;
    doublecomplex* c ;
    VALUE rc ;
    
    get_doublecomplex(a, self) ;
    c = ALLOC(doublecomplex) ;
    rc = wrap_doublecomplex(c) ;

    if( rb_obj_is_kind_of(rb, rb_cDComplex) )
    {
        doublecomplex* b ;
        doublereal bottom ;
        get_doublecomplex(b, rb) ;

        bottom = b->r*b->r + b->i*b->i ;
        
        c->r = a->r*b->r + a->i*b->i ;
        c->i = a->i*b->r - a->r*b->i ;

        c->r /= bottom ;
        c->i /= bottom ;
    }
    else
    {
        doublereal b = (doublereal)NUM2DBL(rb) ;
        c->r = a->r/b ;
        c->i = a->i/b ;
    }
    
    return rc ;
}

#==(rb) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'ext/linalg/dcomplex.c', line 315

VALUE rb_dcomplex_doubleequals(VALUE self, VALUE rb)
{
    doublecomplex* a ;
    get_doublecomplex(a, self) ;

    if( rb_obj_is_kind_of(rb, rb_cDComplex) )
    {
        doublecomplex* b ;
        get_doublecomplex(b, rb) ;

        return (a->r == b->r && a->i == b->i) ? Qtrue : Qfalse ;
    }
    else
    {
        doublereal br = (doublereal)NUM2DBL(rb) ;
        return (a->r == br && a->i == ((doublereal)0.0)) ? Qtrue : Qfalse ;
    }
}

#absObject



289
290
291
292
293
294
# File 'ext/linalg/dcomplex.c', line 289

VALUE rb_dcomplex_abs(VALUE self)
{
    doublecomplex* c ;
    get_doublecomplex(c, self) ;
    return rb_float_new((double)(sqrt(c->r*c->r + c->i*c->i))) ;
}

#abs2Object



296
297
298
299
300
301
# File 'ext/linalg/dcomplex.c', line 296

VALUE rb_dcomplex_abs2(VALUE self)
{
    doublecomplex* c ;
    get_doublecomplex(c, self) ;
    return rb_float_new((double)(c->r*c->r + c->i*c->i)) ;
}

#coerce(other) ⇒ Object



303
304
305
306
# File 'ext/linalg/dcomplex.c', line 303

VALUE rb_dcomplex_coerce(VALUE self, VALUE other)
{
    return rb_assoc_new(self, other) ;
}

#conjObject



275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'ext/linalg/dcomplex.c', line 275

VALUE rb_dcomplex_conj(VALUE self)
{
    doublecomplex* a ;
    doublecomplex* c ;

    get_doublecomplex(a, self) ;
    c = ALLOC(doublecomplex) ;

    c->r = a->r ;
    c->i = -a->i ;
    
    return wrap_doublecomplex(c) ;
}

#imagObject



122
123
124
125
126
127
# File 'ext/linalg/dcomplex.c', line 122

VALUE rb_dcomplex_imag(VALUE self)
{
    doublecomplex* a ;
    get_doublecomplex(a, self) ;
    return rb_float_new((double)a->i) ;
}

#realObject



115
116
117
118
119
120
# File 'ext/linalg/dcomplex.c', line 115

VALUE rb_dcomplex_real(VALUE self)
{
    doublecomplex* a ;
    get_doublecomplex(a, self) ;
    return rb_float_new((double)a->r) ;
}

#to_doublecomplex_ptrObject



308
309
310
311
312
313
# File 'ext/linalg/dcomplex.c', line 308

VALUE rb_dcomplex_to_doublecomplex_ptr(VALUE self)
{
    doublecomplex* c ;
    get_doublecomplex(c, self) ;
    return ULONG2NUM((unsigned long)c) ;
}

#to_sObject



11
12
13
# File 'lib/linalg/dcomplex.rb', line 11

def to_s
   "#{real} + #{imag}i"
end

#within(repsilon, rb) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'ext/linalg/dcomplex.c', line 244

VALUE rb_dcomplex_within(VALUE self, VALUE repsilon, VALUE rb)
{
    doublecomplex* a ;
    doublereal epsilon ;

    get_doublecomplex(a, self) ;
    epsilon = (doublereal)NUM2DBL(repsilon) ;
    
    if( rb_obj_is_kind_of(rb, rb_cDComplex) )
    {
        doublecomplex* b ;
        get_doublecomplex(b, rb) ;
        if( fabs(a->r - b->r) > epsilon ||
            fabs(a->i - b->i) > epsilon )
        {
            return Qfalse ;
        }
    }
    else
    {
        doublereal b = (doublereal)NUM2DBL(rb) ;
        if( fabs(a->r - b) > epsilon ||
            fabs(a->i - (doublereal)0.0) > epsilon )
        {
            return Qfalse ;
        }
    }

    return Qtrue ;
}