[Matrix-SIG] CXX and Numeric

Paul F. Dubois dubois1@llnl.gov
Tue, 1 Jun 1999 12:02:01 -0700


This is a multi-part message in MIME format.

------=_NextPart_000_0007_01BEAC26.8E732C80
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

As part of one of the demos for CXX there is an example defines an Array =
object. Here is a portion of the=20
header file in the Include directory. This is only a demo. I did not =
have the time to do a complete wrap on Numerical's API. A better version =
of this class would be welcome.

Please send mail about CXX direct to me at dubois1@llnl.gov. A friend =
happened to forward your message but otherwise I missed it. Due to the =
large amount of traffic I do not read the python list that carefully. I =
gather you had some complaint about the robustness of an example and my =
failure to do much more on the documentation. I regret my work =
assignment has limited my attention to CXX although it is important to =
me.=20

class Array: public Sequence
{
public:
    virtual bool accepts (PyObject *pyob) const {
        return pyob && PyArray_Check (pyob);
    }
   =20
    explicit Array (PyObject *pyob): Sequence(pyob) {
        validate();
    }
   =20
    Array(const Object& other): Sequence(*other) {
        validate();
    }
   =20
    Array& operator=3D (const Object& rhs) {
        return (*this =3D *rhs);
    }
   =20
    Array& operator=3D (PyObject* rhsp) {
        if(ptr() =3D=3D rhsp) return *this;
        set(rhsp);
        return *this;
    }
   =20
    explicit Array (int n=3D0, PyArray_TYPES t =3D PyArray_DOUBLE)
        : Sequence(FromAPI(PyArray_FromDims(1, &n, t))) {
        validate();
    }

    Array clone() const {
        PyObject *p =3D PyArray_CopyFromObject(ptr(), species(), rank(), =
rank());
        return Array(p);
    }

    int species() const {
        return PyArray_ObjectType(ptr(), 0);
    }

    int rank() const {
        return ((PyArrayObject*) ptr())->nd;
    }

    int dimension(int i) const {
        if (1 <=3D i && i <=3D rank()) {
            return ((PyArrayObject*) ptr())->dimensions[i-1];
        } else {
            return 1;
        }
    }

    int is_contiguous() const {
        return PyArray_ISCONTIGUOUS ((PyArrayObject*) ptr());
    }

    char* to_C() const {
        return ((PyArrayObject*) ptr())->data;
    }

    Array as_contiguous() {
        if (is_contiguous()) return Array(ptr());
        return Array((PyObject*)PyArray_ContiguousFromObject(ptr(), =
species(), 1, 0));
    }       =20
};


------=_NextPart_000_0007_01BEAC26.8E732C80
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.2614.3401" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT size=3D2>As part of one of the demos for CXX there is an =
example=20
defines an Array object. Here is a portion of the </FONT></DIV>
<DIV><FONT size=3D2>header file in the Include directory. This is only a =
demo. I=20
did not have the time to do a complete wrap on Numerical's API. A better =
version=20
of this class would be welcome.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=3D2>Please send mail about CXX direct to me at <A=20
href=3D"mailto:dubois1@llnl.gov">dubois1@llnl.gov</A>. A friend happened =
to=20
forward your message but otherwise I missed it. Due to the large amount =
of=20
traffic I do not read the python list that carefully. I gather you had =
some=20
complaint about the robustness of an example and my failure to do much =
more on=20
the documentation. I regret my work assignment has limited my attention =
to CXX=20
although it is important to me. </FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=3D2>class Array: public=20
Sequence<BR>{<BR>public:<BR>&nbsp;&nbsp;&nbsp; virtual bool accepts =
(PyObject=20
*pyob) const {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return pyob =

&amp;&amp; PyArray_Check (pyob);<BR>&nbsp;&nbsp;&nbsp; =
}<BR>&nbsp;&nbsp;&nbsp;=20
<BR>&nbsp;&nbsp;&nbsp; explicit Array (PyObject *pyob): Sequence(pyob)=20
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
validate();<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; =
<BR>&nbsp;&nbsp;&nbsp;=20
Array(const Object&amp; other): Sequence(*other)=20
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
validate();<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; =
<BR>&nbsp;&nbsp;&nbsp;=20
Array&amp; operator=3D (const Object&amp; rhs)=20
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return (*this =3D=20
*rhs);<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; =
<BR>&nbsp;&nbsp;&nbsp;=20
Array&amp; operator=3D (PyObject* rhsp)=20
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(ptr() =3D=3D rhsp) =
return=20
*this;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
set(rhsp);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return=20
*this;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; =
<BR>&nbsp;&nbsp;&nbsp;=20
explicit Array (int n=3D0, PyArray_TYPES t =3D=20
PyArray_DOUBLE)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :=20
Sequence(FromAPI(PyArray_FromDims(1, &amp;n, t)))=20
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
validate();<BR>&nbsp;&nbsp;&nbsp; }</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=3D2>&nbsp;&nbsp;&nbsp; Array clone() const=20
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PyObject *p =3D=20
PyArray_CopyFromObject(ptr(), species(), rank(),=20
rank());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return=20
Array(p);<BR>&nbsp;&nbsp;&nbsp; }</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=3D2>&nbsp;&nbsp;&nbsp; int species() const=20
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return =
PyArray_ObjectType(ptr(),=20
0);<BR>&nbsp;&nbsp;&nbsp; }</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=3D2>&nbsp;&nbsp;&nbsp; int rank() const=20
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return ((PyArrayObject*) =

ptr())-&gt;nd;<BR>&nbsp;&nbsp;&nbsp; }</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=3D2>&nbsp;&nbsp;&nbsp; int dimension(int i) const=20
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (1 &lt;=3D i =
&amp;&amp; i &lt;=3D=20
rank()) =
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
return ((PyArrayObject*)=20
ptr())-&gt;dimensions[i-1];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
 } else=20
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
return=20
1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; =

}</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=3D2>&nbsp;&nbsp;&nbsp; int is_contiguous() const=20
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return =
PyArray_ISCONTIGUOUS=20
((PyArrayObject*) ptr());<BR>&nbsp;&nbsp;&nbsp; }</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=3D2>&nbsp;&nbsp;&nbsp; char* to_C() const=20
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return ((PyArrayObject*) =

ptr())-&gt;data;<BR>&nbsp;&nbsp;&nbsp; }</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=3D2>&nbsp;&nbsp;&nbsp; Array as_contiguous()=20
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (is_contiguous()) =
return=20
Array(ptr());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return=20
Array((PyObject*)PyArray_ContiguousFromObject(ptr(), species(), 1,=20
0));<BR>&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
<BR>};<BR></FONT></DIV></BODY></HTML>

------=_NextPart_000_0007_01BEAC26.8E732C80--