@@ -4,7 +4,8 @@ use dim4::Dim4;
44use defines:: { AfError , DType , Backend } ;
55use error:: HANDLE_ERROR ;
66use util:: HasAfEnum ;
7- use self :: libc:: { uint8_t, c_void, c_int, c_uint, c_longlong} ;
7+ use self :: libc:: { uint8_t, c_void, c_int, c_uint, c_longlong, c_char} ;
8+ use std:: ffi:: CString ;
89
910type MutAfArray = * mut self :: libc:: c_longlong ;
1011type MutDouble = * mut self :: libc:: c_double ;
@@ -78,6 +79,8 @@ extern {
7879
7980 fn af_print_array ( arr : AfArray ) -> c_int ;
8081
82+ fn af_print_array_gen ( exp : * const c_char , arr : AfArray , precision : c_int ) -> c_int ;
83+
8184 fn af_cast ( out : MutAfArray , arr : AfArray , aftype : uint8_t ) -> c_int ;
8285
8386 fn af_get_backend_id ( backend : * mut c_int , input : AfArray ) -> c_int ;
@@ -417,6 +420,10 @@ impl Drop for Array {
417420
418421/// Print data in the Array
419422///
423+ /// # Parameters
424+ ///
425+ /// - `input` is the Array to be printed
426+ ///
420427/// # Examples
421428///
422429/// ```rust
@@ -438,8 +445,52 @@ impl Drop for Array {
438445/// 0.9251 0.5132 0.6814
439446/// ```
440447pub fn print ( input : & Array ) {
448+ let emptystring = CString :: new ( "" ) . unwrap ( ) ;
449+ unsafe {
450+ let err_val = af_print_array_gen ( emptystring. to_bytes_with_nul ( ) . as_ptr ( ) as * const c_char ,
451+ input. get ( ) as AfArray , 4 ) ;
452+ HANDLE_ERROR ( AfError :: from ( err_val) ) ;
453+ }
454+ }
455+
456+ /// Generalized Array print function
457+ ///
458+ /// Use this function to print Array data with arbitrary preicsion
459+ ///
460+ /// # Parameters
461+ ///
462+ /// - `msg` is message to be printed before printing the Array data
463+ /// - `input` is the Array to be printed
464+ /// - `precision` is data precision with which Array has to be printed
465+ ///
466+ /// # Examples
467+ ///
468+ /// ```rust
469+ /// use arrayfire::{Dim4, print_gen, randu};
470+ /// println!("Create a 5-by-3 matrix of random floats on the GPU");
471+ /// let dims = Dim4::new(&[5, 3, 1, 1]);
472+ /// let a = randu::<f32>(dims);
473+ /// print_gen(String::from("Random Array"), &a, Some(6));
474+ /// ```
475+ ///
476+ /// The sample output will look like below:
477+ ///
478+ /// ```text
479+ /// Random Array
480+ ///
481+ /// [5 3 1 1]
482+ /// 0.740276 0.446440 0.776202
483+ /// 0.921094 0.667321 0.294810
484+ /// 0.039014 0.109939 0.714090
485+ /// 0.969058 0.470269 0.358590
486+ /// 0.925181 0.513225 0.681451
487+ /// ```
488+ pub fn print_gen ( msg : String , input : & Array , precision : Option < i32 > ) {
489+ let emptystring = CString :: new ( msg. as_bytes ( ) ) . unwrap ( ) ;
441490 unsafe {
442- let err_val = af_print_array ( input. get ( ) as AfArray ) ;
491+ let err_val = af_print_array_gen ( emptystring. to_bytes_with_nul ( ) . as_ptr ( ) as * const c_char ,
492+ input. get ( ) as AfArray ,
493+ match precision { Some ( p) =>p, None =>4 } as c_int ) ;
443494 HANDLE_ERROR ( AfError :: from ( err_val) ) ;
444495 }
445496}
0 commit comments