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 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
// // A rust binding for the GSL library by Guillaume Gomez (guillaume1.gomez@gmail.com) // /*! #Vectors Vectors are defined by a gsl_vector structure which describes a slice of a block. Different vectors can be created which point to the same block. A vector slice is a set of equally-spaced elements of an area of memory. The gsl_vector structure contains five components, the size, the stride, a pointer to the memory where the elements are stored, data, a pointer to the block owned by the vector, block, if any, and an ownership flag, owner. The structure is very simple and looks like this, ```C typedef struct { size_t size; size_t stride; double * data; gsl_block * block; int owner; } gsl_vector; ``` The size is simply the number of vector elements. The range of valid indices runs from 0 to size-1. The stride is the step-size from one element to the next in physical memory, measured in units of the appropriate datatype. The pointer data gives the location of the first element of the vector in memory. The pointer block stores the location of the memory block in which the vector elements are located (if any). If the vector owns this block then the owner field is set to one and the block will be deallocated when the vector is freed. If the vector points to a block owned by another object then the owner field is zero and any underlying block will not be deallocated with the vector. !*/ use std::fmt; use std::fmt::{Formatter, Debug}; use ffi; use enums; pub struct VectorView { v: ffi::gsl_vector_view } impl VectorView { /// These functions return a vector view of a subvector of another vector v. The start of the new vector is offset by offset elements /// from the start of the original vector. The new vector has n elements. Mathematically, the i-th element of the new vector v’ is given by, /// /// v'(i) = v->data[(offset + i)*v->stride] /// /// where the index i runs from 0 to n-1. /// /// The data pointer of the returned vector struct is set to null if the combined parameters (offset,n) overrun the end of the original /// vector. /// /// The new vector is only a view of the block underlying the original vector, v. The block containing the elements of v is not owned by /// the new vector. When the view goes out of scope the original vector v and its block will continue to exist. The original memory can /// only be deallocated by freeing the original vector. Of course, the original vector should not be deallocated while the view is still /// in use. /// /// The function gsl_vector_const_subvector is equivalent to gsl_vector_subvector but can be used for vectors which are declared const. pub fn from_vector(v: &VectorF64, offset: usize, n: usize) -> VectorView { unsafe { VectorView { v: ffi::gsl_vector_subvector(v.vec, offset, n) } } } /// These functions return a vector view of a subvector of another vector v with an additional stride argument. The subvector is formed /// in the same way as for gsl_vector_subvector but the new vector has n elements with a step-size of stride from one element to the /// next in the original vector. Mathematically, the i-th element of the new vector v’ is given by, /// /// v'(i) = v->data[(offset + i*stride)*v->stride] /// where the index i runs from 0 to n-1. /// /// Note that subvector views give direct access to the underlying elements of the original vector. For example, the following code will /// zero the even elements of the vector v of length n, while leaving the odd elements untouched, /// /// ```C /// gsl_vector_view v_even /// = gsl_vector_subvector_with_stride (v, 0, 2, n/2); /// gsl_vector_set_zero (&v_even.vector); /// ``` /// A vector view can be passed to any subroutine which takes a vector argument just as a directly allocated vector would be, using &view.vector. /// For example, the following code computes the norm of the odd elements of v using the BLAS routine DNRM2, /// /// ```C /// gsl_vector_view v_odd /// = gsl_vector_subvector_with_stride (v, 1, 2, n/2); /// double r = gsl_blas_dnrm2 (&v_odd.vector); /// ``` /// The function gsl_vector_const_subvector_with_stride is equivalent to gsl_vector_subvector_with_stride but can be used for vectors which /// are declared const. pub fn from_vector_with_stride(v: &VectorF64, offset: usize, stride: usize, n: usize) -> VectorView { unsafe { VectorView { v: ffi::gsl_vector_subvector_with_stride(v.vec, offset, stride, n) } } } /// These functions return a vector view of an array. The start of the new vector is given by base and has n elements. Mathematically, /// the i-th element of the new vector v’ is given by, /// /// v'(i) = base[i] /// /// where the index i runs from 0 to n-1. /// /// The array containing the elements of v is not owned by the new vector view. When the view goes out of scope the original array will /// continue to exist. The original memory can only be deallocated by freeing the original pointer base. Of course, the original array /// should not be deallocated while the view is still in use. /// /// The function gsl_vector_const_view_array is equivalent to gsl_vector_view_array but can be used for arrays which are declared const. pub fn from_array(base: &mut [f64]) -> VectorView { unsafe { VectorView { v: ffi::gsl_vector_view_array(base.as_mut_ptr(), base.len() as usize) } } } /// These functions return a vector view of an array base with an additional stride argument. The subvector is formed in the same way as /// for gsl_vector_view_array but the new vector has n elements with a step-size of stride from one element to the next in the original /// array. Mathematically, the i-th element of the new vector v’ is given by, /// /// v'(i) = base[i*stride] /// /// where the index i runs from 0 to n-1. /// /// Note that the view gives direct access to the underlying elements of the original array. A vector view can be passed to any subroutine /// which takes a vector argument just as a directly allocated vector would be, using &view.vector. /// /// The function gsl_vector_const_view_array_with_stride is equivalent to gsl_vector_view_array_with_stride but can be used for arrays /// which are declared const. pub fn from_array_with_stride(base: &mut [f64], stride: usize) -> VectorView { unsafe { VectorView { v: ffi::gsl_vector_view_array_with_stride(base.as_mut_ptr(), stride, base.len() as usize) } } } pub fn vector(&mut self) -> VectorF64 { unsafe { VectorF64 { vec: ::std::mem::transmute(&mut self.v), can_free: false } } } } pub struct VectorF32 { vec: *mut ffi::gsl_vector_float, can_free: bool } impl VectorF32 { /// create a new VectorF32 with all elements set to zero pub fn new(size: usize) -> Option<VectorF32> { let tmp = unsafe { ffi::gsl_vector_float_calloc(size) }; if tmp.is_null() { None } else { Some(VectorF32 { vec: tmp, can_free: true }) } } pub fn from_slice(slice: &[f32]) -> Option<VectorF32> { let tmp = unsafe { ffi::gsl_vector_float_alloc(slice.len() as usize) }; if tmp.is_null() { None } else { let v = VectorF32 { vec: tmp, can_free: true }; let mut pos = 0usize; for tmp in slice.iter() { v.set(pos, *tmp); pos += 1; } Some(v) } } pub fn len(&self) -> usize { if self.vec.is_null() { 0usize } else { unsafe { (*self.vec).size } } } /// This function returns the i-th element of a vector v. If i lies outside the allowed range of 0 to n-1 then the error handler is invoked and 0 is returned. pub fn get(&self, i: usize) -> f32 { unsafe { ffi::gsl_vector_float_get(self.vec, i) } } /// This function sets the value of the i-th element of a vector v to x. If i lies outside the allowed range of 0 to n-1 then the error handler is invoked. pub fn set(&self, i: usize, x: f32) -> &VectorF32 { unsafe { ffi::gsl_vector_float_set(self.vec, i, x) }; self } /// This function sets all the elements of the vector v to the value x. pub fn set_all(&self, x: f32) -> &VectorF32 { unsafe { ffi::gsl_vector_float_set_all(self.vec, x) }; self } /// This function sets all the elements of the vector v to zero. pub fn set_zero(&self) -> &VectorF32 { unsafe { ffi::gsl_vector_float_set_zero(self.vec) }; self } /// This function makes a basis vector by setting all the elements of the vector v to zero except for the i-th element which is set to one. pub fn set_basis(&self, i: usize) -> &VectorF32 { unsafe { ffi::gsl_vector_float_set_basis(self.vec, i) }; self } /// This function copies the elements of the other vector into the self vector. The two vectors must have the same length. pub fn copy_from(&self, other: &VectorF32) -> enums::Value { unsafe { ffi::gsl_vector_float_memcpy(self.vec, other.vec) } } /// This function copies the elements of the self vector into the other vector. The two vectors must have the same length. pub fn copy_to(&self, other: &VectorF32) -> enums::Value { unsafe { ffi::gsl_vector_float_memcpy(other.vec, self.vec) } } /// This function exchanges the elements of the vectors by copying. The two vectors must have the same length. pub fn swap(&self, other: &VectorF32) -> enums::Value { unsafe { ffi::gsl_vector_float_swap(other.vec, self.vec) } } /// This function exchanges the i-th and j-th elements of the vector v in-place. pub fn swap_elements(&self, i: usize, j: usize) -> enums::Value { unsafe { ffi::gsl_vector_float_swap_elements(self.vec, i, j) } } /// This function reverses the order of the elements of the vector v. pub fn reverse(&self) -> enums::Value { unsafe { ffi::gsl_vector_float_reverse(self.vec) } } /// This function adds the elements of the other vector to the elements of the self vector. /// The result a_i <- a_i + b_i is stored in self and other remains unchanged. The two vectors must have the same length. pub fn add(&self, other: &VectorF32) -> enums::Value { unsafe { ffi::gsl_vector_float_add(self.vec, other.vec) } } /// This function subtracts the elements of the self vector from the elements of the other vector. /// The result a_i <- a_i - b_i is stored in self and other remains unchanged. The two vectors must have the same length. pub fn sub(&self, other: &VectorF32) -> enums::Value { unsafe { ffi::gsl_vector_float_sub(self.vec, other.vec) } } /// This function multiplies the elements of the self vector a by the elements of the other vector. /// The result a_i <- a_i * b_i is stored in self and other remains unchanged. The two vectors must have the same length. pub fn mul(&self, other: &VectorF32) -> enums::Value { unsafe { ffi::gsl_vector_float_mul(self.vec, other.vec) } } /// This function divides the elements of the self vector by the elements of the other vector. /// The result a_i <- a_i / b_i is stored in self and other remains unchanged. The two vectors must have the same length. pub fn div(&self, other: &VectorF32) -> enums::Value { unsafe { ffi::gsl_vector_float_div(self.vec, other.vec) } } /// This function multiplies the elements of the self vector by the constant factor x. The result a_i <- a_i is stored in self. pub fn scale(&self, x: f32) -> enums::Value { unsafe { ffi::gsl_vector_float_scale(self.vec, x) } } /// This function adds the constant value x to the elements of the self vector. The result a_i <- a_i + x is stored in self. pub fn add_constant(&self, x: f32) -> enums::Value { unsafe { ffi::gsl_vector_float_add_constant(self.vec, x) } } /// This function returns the maximum value in the self vector. pub fn max(&self) -> f32 { unsafe { ffi::gsl_vector_float_max(self.vec) } } /// This function returns the minimum value in the self vector. pub fn min(&self) -> f32 { unsafe { ffi::gsl_vector_float_min(self.vec) } } /// This function returns the minimum and maximum values in the self vector, storing them in min_out and max_out. pub fn minmax(&self, min_out: &mut f32, max_out: &mut f32) { unsafe { ffi::gsl_vector_float_minmax(self.vec, min_out, max_out) } } /// This function returns the index of the maximum value in the self vector. /// When there are several equal maximum elements then the lowest index is returned. pub fn max_index(&self) -> usize { unsafe { ffi::gsl_vector_float_max_index(self.vec) } } /// This function returns the index of the minimum value in the self vector. /// When there are several equal minimum elements then the lowest index is returned. pub fn min_index(&self) -> usize { unsafe { ffi::gsl_vector_float_min_index(self.vec) } } /// This function returns the indices of the minimum and maximum values in the self vector, storing them in imin and imax. /// When there are several equal minimum or maximum elements then the lowest indices are returned. pub fn minmax_index(&self) -> (usize, usize) { let mut imin = 0usize; let mut imax = 0usize; unsafe { ffi::gsl_vector_float_minmax_index(self.vec, &mut imin, &mut imax) }; (imin, imax) } /// This function returns true if all the elements of the self vector are equal to 0. pub fn is_null(&self) -> bool { match unsafe { ffi::gsl_vector_float_isnull(self.vec) } { 1 => true, _ => false } } /// This function returns true if all the elements of the self vector are stricly positive. pub fn is_pos(&self) -> bool { match unsafe { ffi::gsl_vector_float_ispos(self.vec) } { 1 => true, _ => false } } /// This function returns true if all the elements of the self vector are stricly negative. pub fn is_neg(&self) -> bool { match unsafe { ffi::gsl_vector_float_isneg(self.vec) } { 1 => true, _ => false } } /// This function returns true if all the elements of the self vector are stricly non-negative. pub fn is_non_neg(&self) -> bool { match unsafe { ffi::gsl_vector_float_isnonneg(self.vec) } { 1 => true, _ => false } } pub fn equal(&self, other: &VectorF32) -> bool { match unsafe { ffi::gsl_vector_float_equal(self.vec, other.vec) } { 1 => true, _ => false } } // I'll find a way to do that later /*pub fn as_slice<'a>(&self) -> &'a [f32] { unsafe { if self.vec.is_null() { let tmp : Vec<f32> = Vec::new(); tmp.as_ref() } else { let tmp : CSlice<f32> = CSlice::new((*self.vec).data, (*self.vec).size as usize); tmp.as_ref() } } }*/ pub fn clone(&self) -> Option<VectorF32> { unsafe { if self.vec.is_null() { None } else { match VectorF32::new((*self.vec).size) { Some(v) => { v.copy_from(self); Some(v) } None => None } } } } } impl Drop for VectorF32 { fn drop(&mut self) { if self.can_free { unsafe { ffi::gsl_vector_float_free(self.vec) }; self.vec = ::std::ptr::null_mut(); } } } impl Debug for VectorF32 { #[allow(unused_must_use)] fn fmt(&self, f: &mut Formatter) -> fmt::Result { unsafe { write!(f, "["); for x in 0usize..(*self.vec).size { if x < (*self.vec).size - 1 { write!(f, "{}, ", self.get(x)); } else { write!(f, "{}", self.get(x)); } } } write!(f, "]") } } impl ffi::FFI<ffi::gsl_vector_float> for VectorF32 { fn wrap(r: *mut ffi::gsl_vector_float) -> VectorF32 { VectorF32 { vec: r, can_free: true } } fn unwrap(v: &VectorF32) -> *mut ffi::gsl_vector_float { v.vec } } pub struct VectorF64 { vec: *mut ffi::gsl_vector, can_free: bool } impl VectorF64 { /// create a new VectorF64 with all elements set to zero pub fn new(size: usize) -> Option<VectorF64> { let tmp = unsafe { ffi::gsl_vector_calloc(size) }; if tmp.is_null() { None } else { Some(VectorF64 { vec: tmp, can_free: true }) } } pub fn from_slice(slice: &[f64]) -> Option<VectorF64> { let tmp = unsafe { ffi::gsl_vector_alloc(slice.len() as usize) }; if tmp.is_null() { None } else { let v = VectorF64 { vec: tmp, can_free: true }; let mut pos = 0usize; for tmp in slice.iter() { v.set(pos, *tmp); pos += 1; } Some(v) } } pub fn len(&self) -> usize { if self.vec.is_null() { 0usize } else { unsafe { (*self.vec).size } } } /// This function returns the i-th element of a vector v. If i lies outside the allowed range of 0 to n-1 then the error handler is invoked and 0 is returned. pub fn get(&self, i: usize) -> f64 { unsafe { ffi::gsl_vector_get(self.vec, i) } } /// This function sets the value of the i-th element of a vector v to x. If i lies outside the allowed range of 0 to n-1 then the error handler is invoked. pub fn set(&self, i: usize, x: f64) -> &VectorF64 { unsafe { ffi::gsl_vector_set(self.vec, i, x) }; self } /// This function sets all the elements of the vector v to the value x. pub fn set_all(&self, x: f64) -> &VectorF64 { unsafe { ffi::gsl_vector_set_all(self.vec, x) }; self } /// This function sets all the elements of the vector v to zero. pub fn set_zero(&self) -> &VectorF64 { unsafe { ffi::gsl_vector_set_zero(self.vec) }; self } /// This function makes a basis vector by setting all the elements of the vector v to zero except for the i-th element which is set to one. pub fn set_basis(&self, i: usize) -> &VectorF64 { unsafe { ffi::gsl_vector_set_basis(self.vec, i) }; self } /// This function copies the elements of the other vector into the self vector. The two vectors must have the same length. pub fn copy_from(&self, other: &VectorF64) -> enums::Value { unsafe { ffi::gsl_vector_memcpy(self.vec, other.vec) } } /// This function copies the elements of the self vector into the other vector. The two vectors must have the same length. pub fn copy_to(&self, other: &VectorF64) -> enums::Value { unsafe { ffi::gsl_vector_memcpy(other.vec, self.vec) } } /// This function exchanges the elements of the vectors by copying. The two vectors must have the same length. pub fn swap(&self, other: &VectorF64) -> enums::Value { unsafe { ffi::gsl_vector_swap(other.vec, self.vec) } } /// This function exchanges the i-th and j-th elements of the vector v in-place. pub fn swap_elements(&self, i: usize, j: usize) -> enums::Value { unsafe { ffi::gsl_vector_swap_elements(self.vec, i, j) } } /// This function reverses the order of the elements of the vector v. pub fn reverse(&self) -> enums::Value { unsafe { ffi::gsl_vector_reverse(self.vec) } } /// This function adds the elements of the other vector to the elements of the self vector. /// The result a_i <- a_i + b_i is stored in self and other remains unchanged. The two vectors must have the same length. pub fn add(&self, other: &VectorF64) -> enums::Value { unsafe { ffi::gsl_vector_add(self.vec, other.vec) } } /// This function subtracts the elements of the self vector from the elements of the other vector. /// The result a_i <- a_i - b_i is stored in self and other remains unchanged. The two vectors must have the same length. pub fn sub(&self, other: &VectorF64) -> enums::Value { unsafe { ffi::gsl_vector_sub(self.vec, other.vec) } } /// This function multiplies the elements of the self vector a by the elements of the other vector. /// The result a_i <- a_i * b_i is stored in self and other remains unchanged. The two vectors must have the same length. pub fn mul(&self, other: &VectorF64) -> enums::Value { unsafe { ffi::gsl_vector_mul(self.vec, other.vec) } } /// This function divides the elements of the self vector by the elements of the other vector. /// The result a_i <- a_i / b_i is stored in self and other remains unchanged. The two vectors must have the same length. pub fn div(&self, other: &VectorF64) -> enums::Value { unsafe { ffi::gsl_vector_div(self.vec, other.vec) } } /// This function multiplies the elements of the self vector by the constant factor x. The result a_i <- a_i is stored in self. pub fn scale(&self, x: f64) -> enums::Value { unsafe { ffi::gsl_vector_scale(self.vec, x) } } /// This function adds the constant value x to the elements of the self vector. The result a_i <- a_i + x is stored in self. pub fn add_constant(&self, x: f64) -> enums::Value { unsafe { ffi::gsl_vector_add_constant(self.vec, x) } } /// This function returns the maximum value in the self vector. pub fn max(&self) -> f64 { unsafe { ffi::gsl_vector_max(self.vec) } } /// This function returns the minimum value in the self vector. pub fn min(&self) -> f64 { unsafe { ffi::gsl_vector_min(self.vec) } } /// This function returns the minimum and maximum values in the self vector, storing them in min_out and max_out. pub fn minmax(&self, min_out: &mut f64, max_out: &mut f64) { unsafe { ffi::gsl_vector_minmax(self.vec, min_out, max_out) } } /// This function returns the index of the maximum value in the self vector. /// When there are several equal maximum elements then the lowest index is returned. pub fn max_index(&self) -> usize { unsafe { ffi::gsl_vector_max_index(self.vec) } } /// This function returns the index of the minimum value in the self vector. /// When there are several equal minimum elements then the lowest index is returned. pub fn min_index(&self) -> usize { unsafe { ffi::gsl_vector_min_index(self.vec) } } /// This function returns the indices of the minimum and maximum values in the self vector, storing them in imin and imax. /// When there are several equal minimum or maximum elements then the lowest indices are returned. pub fn minmax_index(&self) -> (usize, usize) { let mut imin = 0usize; let mut imax = 0usize; unsafe { ffi::gsl_vector_minmax_index(self.vec, &mut imin, &mut imax) }; (imin, imax) } /// This function returns true if all the elements of the self vector are equal to 0. pub fn is_null(&self) -> bool { match unsafe { ffi::gsl_vector_isnull(self.vec) } { 1 => true, _ => false } } /// This function returns true if all the elements of the self vector are stricly positive. pub fn is_pos(&self) -> bool { match unsafe { ffi::gsl_vector_ispos(self.vec) } { 1 => true, _ => false } } /// This function returns true if all the elements of the self vector are stricly negative. pub fn is_neg(&self) -> bool { match unsafe { ffi::gsl_vector_isneg(self.vec) } { 1 => true, _ => false } } /// This function returns true if all the elements of the self vector are stricly non-negative. pub fn is_non_neg(&self) -> bool { match unsafe { ffi::gsl_vector_isnonneg(self.vec) } { 1 => true, _ => false } } pub fn equal(&self, other: &VectorF64) -> bool { match unsafe { ffi::gsl_vector_equal(self.vec, other.vec) } { 1 => true, _ => false } } // I'll find a way to do that later /*pub fn as_slice<'a>(&self) -> &'a [f64] { unsafe { if self.vec.is_null() { let tmp : Vec<f64> = Vec::new(); tmp.as_ref() } else { let tmp : CSlice<f64> = CSlice::new((*self.vec).data, (*self.vec).size as usize); tmp.as_ref() } } }*/ pub fn clone(&self) -> Option<VectorF64> { unsafe { if self.vec.is_null() { None } else { match VectorF64::new((*self.vec).size) { Some(v) => { v.copy_from(self); Some(v) } None => None } } } } } impl Drop for VectorF64 { fn drop(&mut self) { if self.can_free { unsafe { ffi::gsl_vector_free(self.vec) }; self.vec = ::std::ptr::null_mut(); } } } impl Debug for VectorF64 { #[allow(unused_must_use)] fn fmt(&self, f: &mut Formatter) -> fmt::Result { unsafe { write!(f, "["); for x in 0usize..(*self.vec).size { if x < (*self.vec).size - 1 { write!(f, "{}, ", self.get(x)); } else { write!(f, "{}", self.get(x)); } } } write!(f, "]") } } impl ffi::FFI<ffi::gsl_vector> for VectorF64 { fn wrap(r: *mut ffi::gsl_vector) -> VectorF64 { VectorF64 { vec: r, can_free: true } } fn unwrap(v: &VectorF64) -> *mut ffi::gsl_vector { v.vec } } pub fn wrap(v: *mut ffi::gsl_vector) -> VectorF64 { VectorF64 { vec: v, can_free: false } }