about logarithm in .cc files

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

about logarithm in .cc files

时刻
Hello all,
 I am working with Oct-Files, and  encountering a problem, that is, how to write logarithm in .cc files. In .m files it is simple, i.e. log(x), here I set x to be a matrix. So how to do it in .cc files?
Thanks

Best regards,


 
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: about logarithm in .cc files

andy buckle
On Wed, Jan 5, 2011 at 3:21 PM, 时刻 <[hidden email]> wrote:
>
> Hello all,
>  I am working with Oct-Files, and  encountering a problem, that is, how to write logarithm in .cc files. In .m files it is simple, i.e. log(x), here I set x to be a matrix. So how to do it in .cc files?
> Thanks
>
> Best regards,

The trick seems to be to make it into an octave_value first.

#include "octave/oct.h"

DEFUN_DLD (log, args, nargout,"") {
  octave_value_list retval;

  const int rows=3, cols=4;

  dim_vector dv(rows,cols);
  NDArray arr(dv);
  double * pix_p=(double *)arr.fortran_vec();

  for (uint32_t i=0; i<rows*cols ;i++) {
    pix_p[i] = i;
  }

  octave_value arrval(arr);

  return arrval.log();
}



--
/* andy buckle */
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: about logarithm in .cc files

Jaroslav Hajek-2
2011/1/5 Andy Buckle <[hidden email]>:

> On Wed, Jan 5, 2011 at 3:21 PM, 时刻 <[hidden email]> wrote:
>>
>> Hello all,
>>  I am working with Oct-Files, and  encountering a problem, that is, how to write logarithm in .cc files. In .m files it is simple, i.e. log(x), here I set x to be a matrix. So how to do it in .cc files?
>> Thanks
>>
>> Best regards,
>
> The trick seems to be to make it into an octave_value first.
>
> #include "octave/oct.h"
>
> DEFUN_DLD (log, args, nargout,"") {
>  octave_value_list retval;
>
>  const int rows=3, cols=4;
>
>  dim_vector dv(rows,cols);
>  NDArray arr(dv);
>  double * pix_p=(double *)arr.fortran_vec();
>
>  for (uint32_t i=0; i<rows*cols ;i++) {
>    pix_p[i] = i;
>  }
>
>  octave_value arrval(arr);
>
>  return arrval.log();
> }
>


You can do it for NDArray directly:
arr.map<double>(log);

but it won't give you the automatic real->complex promotion if there
are negative elements.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave