[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> </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> </DIV>
<DIV><FONT size=3D2>class Array: public=20
Sequence<BR>{<BR>public:<BR> virtual bool accepts =
(PyObject=20
*pyob) const {<BR> return pyob =
&& PyArray_Check (pyob);<BR> =
}<BR> =20
<BR> explicit Array (PyObject *pyob): Sequence(pyob)=20
{<BR> =20
validate();<BR> }<BR> =
<BR> =20
Array(const Object& other): Sequence(*other)=20
{<BR> =20
validate();<BR> }<BR> =
<BR> =20
Array& operator=3D (const Object& rhs)=20
{<BR> return (*this =3D=20
*rhs);<BR> }<BR> =
<BR> =20
Array& operator=3D (PyObject* rhsp)=20
{<BR> if(ptr() =3D=3D rhsp) =
return=20
*this;<BR> =20
set(rhsp);<BR> return=20
*this;<BR> }<BR> =
<BR> =20
explicit Array (int n=3D0, PyArray_TYPES t =3D=20
PyArray_DOUBLE)<BR> :=20
Sequence(FromAPI(PyArray_FromDims(1, &n, t)))=20
{<BR> =20
validate();<BR> }</FONT></DIV>
<DIV> </DIV>
<DIV><FONT size=3D2> Array clone() const=20
{<BR> PyObject *p =3D=20
PyArray_CopyFromObject(ptr(), species(), rank(),=20
rank());<BR> return=20
Array(p);<BR> }</FONT></DIV>
<DIV> </DIV>
<DIV><FONT size=3D2> int species() const=20
{<BR> return =
PyArray_ObjectType(ptr(),=20
0);<BR> }</FONT></DIV>
<DIV> </DIV>
<DIV><FONT size=3D2> int rank() const=20
{<BR> return ((PyArrayObject*) =
ptr())->nd;<BR> }</FONT></DIV>
<DIV> </DIV>
<DIV><FONT size=3D2> int dimension(int i) const=20
{<BR> if (1 <=3D i =
&& i <=3D=20
rank()) =
{<BR> =20
return ((PyArrayObject*)=20
ptr())->dimensions[i-1];<BR> =
} else=20
{<BR> =
return=20
1;<BR> }<BR> =
}</FONT></DIV>
<DIV> </DIV>
<DIV><FONT size=3D2> int is_contiguous() const=20
{<BR> return =
PyArray_ISCONTIGUOUS=20
((PyArrayObject*) ptr());<BR> }</FONT></DIV>
<DIV> </DIV>
<DIV><FONT size=3D2> char* to_C() const=20
{<BR> return ((PyArrayObject*) =
ptr())->data;<BR> }</FONT></DIV>
<DIV> </DIV>
<DIV><FONT size=3D2> Array as_contiguous()=20
{<BR> if (is_contiguous()) =
return=20
Array(ptr());<BR> return=20
Array((PyObject*)PyArray_ContiguousFromObject(ptr(), species(), 1,=20
0));<BR> } =20
<BR>};<BR></FONT></DIV></BODY></HTML>
------=_NextPart_000_0007_01BEAC26.8E732C80--