mirror of
https://github.com/boostorg/python.git
synced 2026-01-26 06:42:27 +00:00
Mapping of std::complex<T> <-> Python complex.
[SVN r9305]
This commit is contained in:
@@ -63,6 +63,7 @@ $(BPL_EXA)/getting_started3.cpp \
|
||||
$(BPL_EXA)/getting_started4.cpp \
|
||||
$(BPL_EXA)/getting_started5.cpp \
|
||||
$(BPL_EXA)/passing_char.cpp \
|
||||
$(BPL_EXA)/cmplx.cpp \
|
||||
$(BPL_EXA)/ivect.cpp \
|
||||
$(BPL_EXA)/ivect.h \
|
||||
$(BPL_EXA)/noncopyable_export.cpp \
|
||||
@@ -75,6 +76,7 @@ $(BPL_EXA)/test_getting_started3.py \
|
||||
$(BPL_EXA)/test_getting_started4.py \
|
||||
$(BPL_EXA)/test_getting_started5.py \
|
||||
$(BPL_EXA)/tst_passing_char.py \
|
||||
$(BPL_EXA)/tst_cmplx.py \
|
||||
$(BPL_EXA)/tst_dvect.py \
|
||||
$(BPL_EXA)/tst_ivect.py \
|
||||
$(BPL_EXA)/tst_noncopyable.py
|
||||
@@ -88,7 +90,7 @@ OBJ = classes.o conversions.o extension_class.o functions.o \
|
||||
all: libbpl.a test.so abstract.so \
|
||||
getting_started1.so getting_started2.so getting_started3.so \
|
||||
getting_started4.so getting_started5.so \
|
||||
passing_char.so \
|
||||
passing_char.so cmplx.so \
|
||||
noncopyable_export.so noncopyable_import.so \
|
||||
ivect.so dvect.so
|
||||
|
||||
@@ -121,7 +123,7 @@ libbpl.a: $(OBJ)
|
||||
ar r libbpl.a $(OBJ)
|
||||
|
||||
test.so: $(OBJ) comprehensive.o
|
||||
$(LD) $(LDOPTS) $(OBJ) comprehensive.o -o test.so
|
||||
$(LD) $(LDOPTS) $(OBJ) comprehensive.o -o test.so -lm
|
||||
|
||||
abstract.so: $(OBJ) abstract.o
|
||||
$(LD) $(LDOPTS) $(OBJ) abstract.o -o abstract.so
|
||||
@@ -144,6 +146,9 @@ getting_started5.so: $(OBJ) getting_started5.o
|
||||
passing_char.so: $(OBJ) passing_char.o
|
||||
$(LD) $(LDOPTS) $(OBJ) passing_char.o -o passing_char.so
|
||||
|
||||
cmplx.so: $(OBJ) cmplx.o
|
||||
$(LD) $(LDOPTS) $(OBJ) cmplx.o -o cmplx.so -lm
|
||||
|
||||
noncopyable_export.so: $(OBJ) noncopyable_export.o
|
||||
$(LD) $(LDOPTS) $(OBJ) $(HIDDEN) noncopyable_export.o -o noncopyable_export.so
|
||||
|
||||
@@ -180,6 +185,7 @@ clean:
|
||||
rm -f getting_started4.o getting_started4.so
|
||||
rm -f getting_started5.o getting_started5.so
|
||||
rm -f passing_char.o passing_char.so
|
||||
rm -f cmplx.o cmplx.so
|
||||
rm -f noncopyable_export.o noncopyable_export.so
|
||||
rm -f noncopyable_import.o noncopyable_import.so
|
||||
rm -f ivect.o ivect.so
|
||||
|
||||
@@ -66,6 +66,7 @@ $(BPL_EXA)/getting_started3.cpp \
|
||||
$(BPL_EXA)/getting_started4.cpp \
|
||||
$(BPL_EXA)/getting_started5.cpp \
|
||||
$(BPL_EXA)/passing_char.cpp \
|
||||
$(BPL_EXA)/cmplx.cpp \
|
||||
$(BPL_EXA)/ivect.cpp \
|
||||
$(BPL_EXA)/ivect.h \
|
||||
$(BPL_EXA)/noncopyable_export.cpp \
|
||||
@@ -78,6 +79,7 @@ $(BPL_EXA)/test_getting_started3.py \
|
||||
$(BPL_EXA)/test_getting_started4.py \
|
||||
$(BPL_EXA)/test_getting_started5.py \
|
||||
$(BPL_EXA)/tst_passing_char.py \
|
||||
$(BPL_EXA)/tst_cmplx.py \
|
||||
$(BPL_EXA)/tst_dvect.py \
|
||||
$(BPL_EXA)/tst_ivect.py \
|
||||
$(BPL_EXA)/tst_noncopyable.py
|
||||
@@ -91,7 +93,7 @@ OBJ = classes.o conversions.o extension_class.o functions.o \
|
||||
all: libbpl.a test.so abstract.so \
|
||||
getting_started1.so getting_started2.so getting_started3.so \
|
||||
getting_started4.so getting_started5.so \
|
||||
passing_char.so \
|
||||
passing_char.so cmplx.so \
|
||||
noncopyable_export.so noncopyable_import.so \
|
||||
ivect.so dvect.so
|
||||
|
||||
@@ -149,6 +151,9 @@ getting_started5.so: $(OBJ) getting_started5.o
|
||||
passing_char.so: $(OBJ) passing_char.o
|
||||
$(LD) $(LDOPTS) $(OBJ) passing_char.o -o passing_char.so
|
||||
|
||||
cmplx.so: $(OBJ) cmplx.o
|
||||
$(LD) $(LDOPTS) $(OBJ) cmplx.o -o cmplx.so -lm
|
||||
|
||||
noncopyable_export.so: $(OBJ) noncopyable_export.o
|
||||
$(LD) $(LDOPTS) $(OBJ) $(HIDDEN) noncopyable_export.o -o noncopyable_export.so
|
||||
|
||||
@@ -185,6 +190,7 @@ clean:
|
||||
rm -f getting_started4.o getting_started4.so
|
||||
rm -f getting_started5.o getting_started5.so
|
||||
rm -f passing_char.o passing_char.so
|
||||
rm -f cmplx.o cmplx.so
|
||||
rm -f noncopyable_export.o noncopyable_export.so
|
||||
rm -f noncopyable_import.o noncopyable_import.so
|
||||
rm -f ivect.o ivect.so
|
||||
|
||||
42
example/cmplx.cpp
Normal file
42
example/cmplx.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <iostream>
|
||||
#include <complex>
|
||||
|
||||
namespace { // Avoid cluttering the global namespace.
|
||||
|
||||
std::complex<double> dpolar(double rho, double theta) {
|
||||
return std::polar(rho, theta);
|
||||
}
|
||||
double dreal(const std::complex<double>& c) { return c.real(); }
|
||||
double dimag(std::complex<double> c) { return c.imag(); }
|
||||
|
||||
std::complex<float> fpolar(float rho, float theta) {
|
||||
return std::polar(rho, theta);
|
||||
}
|
||||
double freal(const std::complex<float>& c) { return c.real(); }
|
||||
double fimag(std::complex<float> c) { return c.imag(); }
|
||||
}
|
||||
|
||||
#include <boost/python/class_builder.hpp>
|
||||
namespace python = boost::python;
|
||||
|
||||
extern "C"
|
||||
DL_EXPORT(void)
|
||||
initcmplx()
|
||||
{
|
||||
try
|
||||
{
|
||||
// Create an object representing this extension module.
|
||||
python::module_builder this_module("cmplx");
|
||||
|
||||
this_module.def(dpolar, "dpolar");
|
||||
this_module.def(dreal, "dreal");
|
||||
this_module.def(dimag, "dimag");
|
||||
this_module.def(fpolar, "fpolar");
|
||||
this_module.def(freal, "freal");
|
||||
this_module.def(fimag, "fimag");
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
python::handle_exception(); // Deal with the exception for Python
|
||||
}
|
||||
}
|
||||
20
example/tst_cmplx.py
Normal file
20
example/tst_cmplx.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import cmplx
|
||||
c = cmplx.dpolar(1, 1)
|
||||
print c
|
||||
print cmplx.dreal(c)
|
||||
print cmplx.dimag(c)
|
||||
|
||||
c = cmplx.fpolar(1, 1)
|
||||
print c
|
||||
print cmplx.freal(c)
|
||||
print cmplx.fimag(c)
|
||||
|
||||
print cmplx.dreal(c)
|
||||
print cmplx.dimag(c)
|
||||
|
||||
try:
|
||||
cmplx.freal("")
|
||||
except TypeError:
|
||||
pass
|
||||
else:
|
||||
raise SystemError
|
||||
Reference in New Issue
Block a user