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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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
243
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
//
// A rust binding for the GSL library by Guillaume Gomez (guillaume1.gomez@gmail.com)
//

use std::fmt;
use std::fmt::{Formatter, Debug};
use types::{ComplexF32, ComplexF64};
use types::{VectorComplexF64, VectorComplexF32};
use ffi;
use enums;

pub struct MatrixComplexF64 {
    mat: *mut ffi::gsl_matrix_complex
}

impl MatrixComplexF64 {
    /// Creates a new MatrixF64 with all elements set to zero
    /// 
    /// Example with n1 = 2 and n2 = 3 :
    /// 
    /// XX XX XX
    /// 
    /// XX XX XX
    pub fn new(n1: usize, n2: usize) -> Option<MatrixComplexF64> {
        let tmp = unsafe { ffi::gsl_matrix_complex_calloc(n1, n2) };

        if tmp.is_null() {
            None
        } else {
            Some(MatrixComplexF64 {
                mat: tmp
            })
        }
    }

    /// This function returns the (i,j)-th element of the matrix.
    /// If y or x lie outside the allowed range of 0 to n1-1 and 0 to n2-1 then the error handler is invoked and 0 is returned.
    pub fn get(&self, y: usize, x: usize) -> ComplexF64 {
        unsafe { ::std::mem::transmute(ffi::gsl_matrix_complex_get(self.mat, y, x)) }
    }

    /// This function sets the value of the (i,j)-th element of the matrix to value.
    /// If y or x lies outside the allowed range of 0 to n1-1 and 0 to n2-1 then the error handler is invoked.
    pub fn set(&mut self, y: usize, x: usize, value: &ComplexF64) -> &MatrixComplexF64 {
        unsafe { ffi::gsl_matrix_complex_set(self.mat, y, x, ::std::mem::transmute(*value)) };
        self
    }

    /// This function sets all the elements of the matrix to the value x.
    pub fn set_all(&mut self, x: &ComplexF64) -> &MatrixComplexF64 {
        unsafe { ffi::gsl_matrix_complex_set_all(self.mat, ::std::mem::transmute(*x)) };
        self
    }

    /// This function sets all the elements of the matrix to zero.
    pub fn set_zero(&mut self) -> &MatrixComplexF64 {
        unsafe { ffi::gsl_matrix_complex_set_zero(self.mat) };
        self
    }

    /// This function sets the elements of the matrix to the corresponding elements of the identity matrix, m(i,j) = \delta(i,j), i.e. a unit diagonal with all off-diagonal elements zero.
    /// This applies to both square and rectangular matrices.
    pub fn set_identity(&mut self) -> &MatrixComplexF64 {
        unsafe { ffi::gsl_matrix_complex_set_identity(self.mat) };
        self
    }

    /// This function copies the elements of the other matrix into the self matrix. The two matrices must have the same size.
    pub fn copy_from(&mut self, other: &MatrixComplexF64) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_memcpy(self.mat, other.mat) }
    }

    /// This function copies the elements of the self matrix into the other matrix. The two matrices must have the same size.
    pub fn copy_to(&self, other: &mut MatrixComplexF64) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_memcpy(other.mat, self.mat) }
    }

    /// This function exchanges the elements of the matrices self and other by copying. The two matrices must have the same size.
    pub fn swap(&mut self, other: &mut MatrixComplexF64) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_swap(self.mat, other.mat) }
    }

    /// This function copies the elements of the y-th row of the matrix into the returned vector.
    pub fn get_row(&self, y: usize) -> Option<(VectorComplexF64, enums::Value)> {
        let tmp = unsafe { ffi::gsl_vector_complex_alloc((*self.mat).size2) };

        if tmp.is_null() {
            None
        } else {
            let ret = unsafe { ffi::gsl_matrix_complex_get_row(tmp, self.mat, y) };

            Some((ffi::FFI::wrap(tmp), ret))
        }
    }

    /// This function copies the elements of the x-th column of the matrix into the returned vector.
    pub fn get_col(&self, x: usize) -> Option<(VectorComplexF64, enums::Value)> {
        let tmp = unsafe { ffi::gsl_vector_complex_alloc((*self.mat).size1) };

        if tmp.is_null() {
            None
        } else {
            let ret = unsafe { ffi::gsl_matrix_complex_get_col(tmp, self.mat, x) };

            Some((ffi::FFI::wrap(tmp), ret))
        }
    }

    /// This function copies the elements of the vector v into the y-th row of the matrix.
    /// The length of the vector must be the same as the length of the row.
    pub fn set_row(&mut self, y: usize, v: &VectorComplexF64) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_set_row(self.mat, y, ffi::FFI::unwrap_shared(v)) }
    }

    /// This function copies the elements of the vector v into the x-th column of the matrix.
    /// The length of the vector must be the same as the length of the column.
    pub fn set_col(&mut self, x: usize, v: &VectorComplexF64) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_set_col(self.mat, x, ffi::FFI::unwrap_shared(v)) }
    }

    /// This function exchanges the y1-th and y2-th rows of the matrix in-place.
    pub fn swap_rows(&mut self, y1: usize, y2: usize) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_swap_rows(self.mat, y1, y2) }
    }

    /// This function exchanges the x1-th and x2-th columns of the matrix in-place.
    pub fn swap_columns(&mut self, x1: usize, x2: usize) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_swap_columns(self.mat, x1, x2) }
    }

    /// This function exchanges the i-th row and j-th column of the matrix in-place. The matrix must be square for this operation to be possible.
    pub fn swap_row_col(&mut self, i: usize, j: usize) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_swap_rowcol(self.mat, i, j) }
    }

    /// This function returns the transpose of the matrix by copying the elements into it.
    /// This function works for all matrices provided that the dimensions of the matrix dest match the transposed dimensions of the matrix.
    pub fn transpose_memcpy(&self) -> Option<(MatrixComplexF64, enums::Value)> {
        let dest = unsafe { ffi::gsl_matrix_complex_alloc((*self.mat).size1, (*self.mat).size2) };

        if dest.is_null() {
            None
        } else {
            let ret = unsafe { ffi::gsl_matrix_complex_transpose_memcpy(dest, self.mat) };

            Some((MatrixComplexF64{mat: dest}, ret))
        }
    }

    /// This function replaces the matrix m by its transpose by copying the elements of the matrix in-place.
    /// The matrix must be square for this operation to be possible.
    pub fn transpose(&self) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_transpose(self.mat) }
    }

    /// This function adds the elements of the other matrix to the elements of the self matrix.
    /// The result self(i,j) <- self(i,j) + other(i,j) is stored in self and other remains unchanged. The two matrices must have the same dimensions.
    pub fn add(&mut self, other: &MatrixComplexF64) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_add(self.mat, other.mat) }
    }

    /// This function subtracts the elements of the other matrix from the elements of the self matrix.
    /// The result self(i,j) <- self(i,j) - other(i,j) is stored in self and other remains unchanged. The two matrices must have the same dimensions.
    pub fn sub(&mut self, other: &MatrixComplexF64) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_sub(self.mat, other.mat) }
    }

    /// This function multiplies the elements of the self matrix by the elements of the other matrix.
    /// The result self(i,j) <- self(i,j) * other(i,j) is stored in self and other remains unchanged. The two matrices must have the same dimensions.
    pub fn mul_elements(&mut self, other: &MatrixComplexF64) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_mul_elements(self.mat, other.mat) }
    }

    /// This function divides the elements of the self matrix by the elements of the other matrix.
    /// The result self(i,j) <- self(i,j) / other(i,j) is stored in self and other remains unchanged. The two matrices must have the same dimensions.
    pub fn div_elements(&mut self, other: &MatrixComplexF64) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_div_elements(self.mat, other.mat) }
    }

    /// This function multiplies the elements of the self matrix by the constant factor x. The result self(i,j) <- x self(i,j) is stored in self.
    pub fn scale(&mut self, x: &ComplexF64) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_scale(self.mat, ::std::mem::transmute(*x)) }
    }

    /// This function adds the constant value x to the elements of the self matrix. The result self(i,j) <- self(i,j) + x is stored in self.
    pub fn add_constant(&mut self, x: &ComplexF64) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_add_constant(self.mat, ::std::mem::transmute(*x)) }
    }

    /// This function returns true if all the elements of the self matrix are stricly zero.
    pub fn is_null(&self) -> bool {
        match unsafe { ffi::gsl_matrix_complex_isnull(self.mat) } {
            1 => true,
            _ => false
        }
    }

    /// This function returns true if all the elements of the self matrix are stricly positive.
    pub fn is_pos(&self) -> bool {
        match unsafe { ffi::gsl_matrix_complex_ispos(self.mat) } {
            1 => true,
            _ => false
        }
    }

    /// This function returns true if all the elements of the self matrix are stricly negative.
    pub fn is_neg(&self) -> bool {
        match unsafe { ffi::gsl_matrix_complex_isneg(self.mat) } {
            1 => true,
            _ => false
        }
    }

    /// This function returns true if all the elements of the self matrix are stricly non-negative.
    pub fn is_non_neg(&self) -> bool {
        match unsafe { ffi::gsl_matrix_complex_isnonneg(self.mat) } {
            1 => true,
            _ => false
        }
    }

    /// This function returns true if all elements of the two matrix are equal.
    pub fn equal(&self, other: &MatrixComplexF64) -> bool {
        match unsafe { ffi::gsl_matrix_complex_equal(self.mat, other.mat) } {
            1 => true,
            _ => false
        }
    }

    pub fn clone(&self) -> Option<MatrixComplexF64> {
        unsafe {
            if self.mat.is_null() {
                None
            } else {
                match MatrixComplexF64::new((*self.mat).size1, (*self.mat).size2) {
                    Some(mut m) => {
                        m.copy_from(self);
                        Some(m)
                    }
                    None => None
                }
            }
        }
    }
}

impl Drop for MatrixComplexF64 {
    fn drop(&mut self) {
        unsafe { ffi::gsl_matrix_complex_free(self.mat) };
        self.mat = ::std::ptr::null_mut();
    }
}

impl Debug for MatrixComplexF64 {
    #[allow(unused_must_use)]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        unsafe {
            for y in 0usize..(*self.mat).size1 {
                write!(f, "[");
                for x in 0usize..(*self.mat).size2 {
                    if x < (*self.mat).size2 - 1 {
                        write!(f, "{:?}, ", self.get(y, x));
                    } else {
                        write!(f, "{:?}", self.get(y, x));
                    }
                }
                if y < (*self.mat).size1 - 1 {
                    write!(f, "]\n");
                }
            }
        }
        write!(f, "]")
    }
}

impl ffi::FFI<ffi::gsl_matrix_complex> for MatrixComplexF64 {
    fn wrap(r: *mut ffi::gsl_matrix_complex) -> MatrixComplexF64 {
        MatrixComplexF64 {
            mat: r
        }
    }

    fn soft_wrap(r: *mut ffi::gsl_matrix_complex) -> MatrixComplexF64 {
        Self::wrap(r)
    }

    fn unwrap_shared(m: &MatrixComplexF64) -> *const ffi::gsl_matrix_complex {
        m.mat as *const _
    }

    fn unwrap_unique(m: &mut MatrixComplexF64) -> *mut ffi::gsl_matrix_complex {
        m.mat
    }
}

pub struct MatrixComplexF32 {
    mat: *mut ffi::gsl_matrix_complex_float
}

impl MatrixComplexF32 {
    /// Creates a new MatrixF64 with all elements set to zero
    /// 
    /// Example with n1 = 2 and n2 = 3 :
    /// 
    /// XX XX XX
    /// 
    /// XX XX XX
    pub fn new(n1: usize, n2: usize) -> Option<MatrixComplexF32> {
        let tmp = unsafe { ffi::gsl_matrix_complex_float_calloc(n1, n2) };

        if tmp.is_null() {
            None
        } else {
            Some(MatrixComplexF32 {
                mat: tmp
            })
        }
    }

    /// This function returns the (i,j)-th element of the matrix.
    /// If y or x lie outside the allowed range of 0 to n1-1 and 0 to n2-1 then the error handler is invoked and 0 is returned.
    pub fn get(&self, y: usize, x: usize) -> ComplexF32 {
        unsafe { ::std::mem::transmute(ffi::gsl_matrix_complex_float_get(self.mat, y, x)) }
    }

    /// This function sets the value of the (i,j)-th element of the matrix to value.
    /// If y or x lies outside the allowed range of 0 to n1-1 and 0 to n2-1 then the error handler is invoked.
    pub fn set(&mut self, y: usize, x: usize, value: &ComplexF32) -> &MatrixComplexF32 {
        unsafe { ffi::gsl_matrix_complex_float_set(self.mat, y, x, ::std::mem::transmute(*value)) };
        self
    }

    /// This function sets all the elements of the matrix to the value x.
    pub fn set_all(&mut self, x: &ComplexF32) -> &MatrixComplexF32 {
        unsafe { ffi::gsl_matrix_complex_float_set_all(self.mat, ::std::mem::transmute(*x)) };
        self
    }

    /// This function sets all the elements of the matrix to zero.
    pub fn set_zero(&mut self) -> &MatrixComplexF32 {
        unsafe { ffi::gsl_matrix_complex_float_set_zero(self.mat) };
        self
    }

    /// This function sets the elements of the matrix to the corresponding elements of the identity matrix, m(i,j) = \delta(i,j), i.e. a unit diagonal with all off-diagonal elements zero.
    /// This applies to both square and rectangular matrices.
    pub fn set_identity(&mut self) -> &MatrixComplexF32 {
        unsafe { ffi::gsl_matrix_complex_float_set_identity(self.mat) };
        self
    }

    /// This function copies the elements of the other matrix into the self matrix. The two matrices must have the same size.
    pub fn copy_from(&mut self, other: &MatrixComplexF32) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_float_memcpy(self.mat, other.mat) }
    }

    /// This function copies the elements of the self matrix into the other matrix. The two matrices must have the same size.
    pub fn copy_to(&self, other: &mut MatrixComplexF32) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_float_memcpy(other.mat, self.mat) }
    }

    /// This function exchanges the elements of the matrices self and other by copying. The two matrices must have the same size.
    pub fn swap(&mut self, other: &mut MatrixComplexF32) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_float_swap(self.mat, other.mat) }
    }

    /// This function copies the elements of the y-th row of the matrix into the returned vector.
    pub fn get_row(&self, y: usize) -> Option<(VectorComplexF32, enums::Value)> {
        let tmp = unsafe { ffi::gsl_vector_complex_float_alloc((*self.mat).size2) };

        if tmp.is_null() {
            None
        } else {
            let ret = unsafe { ffi::gsl_matrix_complex_float_get_row(tmp, self.mat, y) };

            Some((ffi::FFI::wrap(tmp), ret))
        }
    }

    /// This function copies the elements of the x-th column of the matrix into the returned vector.
    pub fn get_col(&self, x: usize) -> Option<(VectorComplexF32, enums::Value)> {
        let tmp = unsafe { ffi::gsl_vector_complex_float_alloc((*self.mat).size1) };

        if tmp.is_null() {
            None
        } else {
            let ret = unsafe { ffi::gsl_matrix_complex_float_get_col(tmp, self.mat, x) };

            Some((ffi::FFI::wrap(tmp), ret))
        }
    }

    /// This function copies the elements of the vector v into the y-th row of the matrix.
    /// The length of the vector must be the same as the length of the row.
    pub fn set_row(&mut self, y: usize, v: &VectorComplexF32) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_float_set_row(self.mat, y, ffi::FFI::unwrap_shared(v)) }
    }

    /// This function copies the elements of the vector v into the x-th column of the matrix.
    /// The length of the vector must be the same as the length of the column.
    pub fn set_col(&mut self, x: usize, v: &VectorComplexF32) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_float_set_col(self.mat, x, ffi::FFI::unwrap_shared(v)) }
    }

    /// This function exchanges the y1-th and y2-th rows of the matrix in-place.
    pub fn swap_rows(&self, y1: usize, y2: usize) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_float_swap_rows(self.mat, y1, y2) }
    }

    /// This function exchanges the x1-th and x2-th columns of the matrix in-place.
    pub fn swap_columns(&mut self, x1: usize, x2: usize) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_float_swap_columns(self.mat, x1, x2) }
    }

    /// This function exchanges the i-th row and j-th column of the matrix in-place. The matrix must be square for this operation to be possible.
    pub fn swap_row_col(&mut self, i: usize, j: usize) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_float_swap_rowcol(self.mat, i, j) }
    }

    /// This function returns the transpose of the matrix by copying the elements into it.
    /// This function works for all matrices provided that the dimensions of the matrix dest match the transposed dimensions of the matrix.
    pub fn transpose_memcpy(&self) -> Option<(MatrixComplexF32, enums::Value)> {
        let dest = unsafe { ffi::gsl_matrix_complex_float_alloc((*self.mat).size1, (*self.mat).size2) };

        if dest.is_null() {
            None
        } else {
            let ret = unsafe { ffi::gsl_matrix_complex_float_transpose_memcpy(dest, self.mat) };

            Some((MatrixComplexF32{mat: dest}, ret))
        }
    }

    /// This function replaces the matrix m by its transpose by copying the elements of the matrix in-place.
    /// The matrix must be square for this operation to be possible.
    pub fn transpose(&mut self) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_float_transpose(self.mat) }
    }

    /// This function adds the elements of the other matrix to the elements of the self matrix.
    /// The result self(i,j) <- self(i,j) + other(i,j) is stored in self and other remains unchanged. The two matrices must have the same dimensions.
    pub fn add(&mut self, other: &MatrixComplexF32) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_float_add(self.mat, other.mat) }
    }

    /// This function subtracts the elements of the other matrix from the elements of the self matrix.
    /// The result self(i,j) <- self(i,j) - other(i,j) is stored in self and other remains unchanged. The two matrices must have the same dimensions.
    pub fn sub(&mut self, other: &MatrixComplexF32) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_float_sub(self.mat, other.mat) }
    }

    /// This function multiplies the elements of the self matrix by the elements of the other matrix.
    /// The result self(i,j) <- self(i,j) * other(i,j) is stored in self and other remains unchanged. The two matrices must have the same dimensions.
    pub fn mul_elements(&mut self, other: &MatrixComplexF32) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_float_mul_elements(self.mat, other.mat) }
    }

    /// This function divides the elements of the self matrix by the elements of the other matrix.
    /// The result self(i,j) <- self(i,j) / other(i,j) is stored in self and other remains unchanged. The two matrices must have the same dimensions.
    pub fn div_elements(&mut self, other: &MatrixComplexF32) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_float_div_elements(self.mat, other.mat) }
    }

    /// This function multiplies the elements of the self matrix by the constant factor x. The result self(i,j) <- x self(i,j) is stored in self.
    pub fn scale(&mut self, x: &ComplexF32) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_float_scale(self.mat, ::std::mem::transmute(*x)) }
    }

    /// This function adds the constant value x to the elements of the self matrix. The result self(i,j) <- self(i,j) + x is stored in self.
    pub fn add_constant(&mut self, x: &ComplexF32) -> enums::Value {
        unsafe { ffi::gsl_matrix_complex_float_add_constant(self.mat, ::std::mem::transmute(*x)) }
    }

    /// This function returns true if all the elements of the self matrix are stricly zero.
    pub fn is_null(&self) -> bool {
        match unsafe { ffi::gsl_matrix_complex_float_isnull(self.mat) } {
            1 => true,
            _ => false
        }
    }

    /// This function returns true if all the elements of the self matrix are stricly positive.
    pub fn is_pos(&self) -> bool {
        match unsafe { ffi::gsl_matrix_complex_float_ispos(self.mat) } {
            1 => true,
            _ => false
        }
    }

    /// This function returns true if all the elements of the self matrix are stricly negative.
    pub fn is_neg(&self) -> bool {
        match unsafe { ffi::gsl_matrix_complex_float_isneg(self.mat) } {
            1 => true,
            _ => false
        }
    }

    /// This function returns true if all the elements of the self matrix are stricly non-negative.
    pub fn is_non_neg(&self) -> bool {
        match unsafe { ffi::gsl_matrix_complex_float_isnonneg(self.mat) } {
            1 => true,
            _ => false
        }
    }

    /// This function returns true if all elements of the two matrix are equal.
    pub fn equal(&self, other: &MatrixComplexF32) -> bool {
        match unsafe { ffi::gsl_matrix_complex_float_equal(self.mat,
            other.mat) } {
            1 => true,
            _ => false
        }
    }

    pub fn clone(&self) -> Option<MatrixComplexF32> {
        unsafe {
            if self.mat.is_null() {
                None
            } else {
                match MatrixComplexF32::new((*self.mat).size1, (*self.mat).size2) {
                    Some(mut m) => {
                        m.copy_from(self);
                        Some(m)
                    }
                    None => None
                }
            }
        }
    }
}

impl Drop for MatrixComplexF32 {
    fn drop(&mut self) {
        unsafe { ffi::gsl_matrix_complex_float_free(self.mat) };
        self.mat = ::std::ptr::null_mut();
    }
}

impl Debug for MatrixComplexF32 {
    #[allow(unused_must_use)]
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        unsafe {
            for y in 0usize..(*self.mat).size1 {
                write!(f, "[");
                for x in 0usize..(*self.mat).size2 {
                    if x < (*self.mat).size2 - 1 {
                        write!(f, "{:?}, ", self.get(y, x));
                    } else {
                        write!(f, "{:?}", self.get(y, x));
                    }
                }
                if y < (*self.mat).size1 - 1 {
                    write!(f, "]\n");
                }
            }
        }
        write!(f, "]")
    }
}

impl ffi::FFI<ffi::gsl_matrix_complex_float> for MatrixComplexF32 {
    fn wrap(r: *mut ffi::gsl_matrix_complex_float) -> MatrixComplexF32 {
        MatrixComplexF32 {
            mat: r
        }
    }

    fn soft_wrap(r: *mut ffi::gsl_matrix_complex_float) -> MatrixComplexF32 {
        Self::wrap(r)
    }

    fn unwrap_shared(m: &MatrixComplexF32) -> *const ffi::gsl_matrix_complex_float {
        m.mat as *const _
    }

    fn unwrap_unique(m: &mut MatrixComplexF32) -> *mut ffi::gsl_matrix_complex_float {
        m.mat
    }
}