help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: single/double precision in C++


From: Sergei Steshenko
Subject: Re: single/double precision in C++
Date: Mon, 9 Nov 2009 11:57:37 -0800 (PST)


--- On Mon, 11/9/09, Peter L. Søndergaard <address@hidden> wrote:

> From: Peter L. Søndergaard <address@hidden>
> Subject: single/double precision in C++
> To: "Octave users list" <address@hidden>
> Date: Monday, November 9, 2009, 5:13 AM
> Hello everyone,
> 
> I am writing Octave interfaces for a library I have
> developed (see LTFAT
> on sourceforge). The interface should be able to handle
> both real and
> complex input in both single and double precision.
> 
> This leads to code like the one further down this email.
> The code works,
> but there is a lot of duplication, because I end up having
> 4 different
> cases to handle all situations. In the end, something like
> this is
> necessary, because I need to call 4 different C functions,
> but still I
> would hope that there was a more clever way of writing the
> function.
> 
> Any suggestions would be most welcome, because I have to do
> it for
> around 20 different functions.
> 
> Cheers,
> 
> Peter.
> 
> And here is the current code:
> 
> 
> #include <octave/oct.h>
> #include "config.h"
> #include "ltfat.h"
> 
> DEFUN_DLD (comp_dgt_fac, args, ,
>   "This function calls the C-library\n\
>   c=comp_dgt_fac(f,gf,a,M);\n\
>   Yeah.")
> {
> 
>   const bool f_is_complex  =
> args(0).is_complex_type();
> 
>   const int a = args(2).int_value();
>   const int M = args(3).int_value();
> 
>   if (f_is_complex)
>   {
> 
>      if (args(0).is_double_type())
>      {
>     const ComplexMatrix f =
> args(0).complex_matrix_value();
>     const ComplexMatrix gf =
> args(1).complex_matrix_value();
>     
>     const int L = f.rows();
>     const int W = f.columns();
>     const int R = gf.rows()*gf.columns()/L;
>     
>     const int N = L/a;
>     
>     ComplexMatrix cout(M,N*W*R);  
>     
>    
> dgt_fac((ltfat_complex*)f.data(),(ltfat_complex*)gf.data(),
>         L, W, R, a,
> M,(ltfat_complex*)cout.data());
>     
>     return octave_value (cout);
>      
>      }
>      else
>      {
> 
>     const FloatComplexMatrix f =
> args(0).float_complex_matrix_value();
>     const FloatComplexMatrix gf =
> args(1).float_complex_matrix_value();
>     
>     const int L = f.rows();
>     const int W = f.columns();
>     const int R = gf.rows()*gf.columns()/L;
>     
>     const int N = L/a;
>     
>     FloatComplexMatrix cout(M,N*W*R); 
> 
>     
>    
> sdgt_fac((ltfat_scomplex*)f.data(),(ltfat_scomplex*)gf.data(),
>         L, W, R, a,
> M,(ltfat_scomplex*)cout.data());
>     
>     return octave_value (cout);
>     
>      }
> 
>   }
>   else
>   {
> 
>      if (args(0).is_double_type())
>      {
> 
>     const Matrix f =
> args(0).matrix_value();
>     const ComplexMatrix gf =
> args(1).complex_matrix_value();
>     
>     const int L = f.rows();
>     const int W = f.columns();
>     const int R = gf.rows()*gf.columns()/L;
>     
>     const int N = L/a;
>     
>     ComplexMatrix cout(M,N*W*R);  
>     
>    
> dgt_fac_r((double*)f.data(),(ltfat_complex*)gf.data(),
>           L, W, R, a,
> M,(ltfat_complex*)cout.data());
>     
>     return octave_value (cout);
>     
>      }
>      else
>      {
>     const FloatMatrix f =
> args(0).float_matrix_value();
>     const FloatComplexMatrix gf =
> args(1).float_complex_matrix_value();
>     
>     const int L = f.rows();
>     const int W = f.columns();
>     const int R = gf.rows()*gf.columns()/L;
>     
>     const int N = L/a;
>     
>     FloatComplexMatrix cout(M,N*W*R); 
> 
>     
>    
> sdgt_fac_r((float*)f.data(),(ltfat_scomplex*)gf.data(),
>           L, W, R, a,
> M,(ltfat_scomplex*)cout.data());
>     
>     return octave_value (cout);
>     
>      }  
>   }
> 
> }
> 

I would write a macro (and in C++, I think, it is possible to write a
"fancier" template class/function, though I'm not an expert in C++).

I have a similar (float/double, float complex/double complex) situattion
in my (non-octave) code, and macros serve me pretty well.

Regards,
  Sergei.


      



reply via email to

[Prev in Thread] Current Thread [Next in Thread]