| 33 | NGS_DLL_HEADER void ExportStdMathFunctions(py::module &m); |
| 34 | |
| 35 | shared_ptr<CoefficientFunction> MakeCoefficient (py::object val) |
| 36 | { |
| 37 | py::extract<shared_ptr<CoefficientFunction>> ecf(val); |
| 38 | if (ecf.check()) return ecf(); |
| 39 | |
| 40 | // a numpy.complex converts itself to a real, and prints a warning |
| 41 | // thus we check for it first |
| 42 | // if (string(py::str(val.get_type())) == "<class 'numpy.complex128'>") |
| 43 | if (string(py::str(py::type::of(val))) == "<class 'numpy.complex128'>") |
| 44 | return MakeConstantCoefficientFunction(val.cast<Complex>()); |
| 45 | |
| 46 | try |
| 47 | { |
| 48 | double value = val.cast<double>(); |
| 49 | if (value) |
| 50 | return make_shared<ConstantCoefficientFunction> (value); |
| 51 | else |
| 52 | return ZeroCF(Array<int>()); |
| 53 | } |
| 54 | catch(const py::cast_error&) {} |
| 55 | try { return MakeConstantCoefficientFunction(val.cast<Complex>()); } |
| 56 | catch(const py::cast_error&) {} |
| 57 | |
| 58 | if (py::isinstance<py::list>(val)) |
| 59 | { |
| 60 | auto el = py::cast<py::list>(val); |
| 61 | Array<shared_ptr<CoefficientFunction>> cflist(el.size()); |
| 62 | for (int i : Range(cflist.Size())) |
| 63 | cflist[i] = MakeCoefficient(el[i]); |
| 64 | return MakeDomainWiseCoefficientFunction(std::move(cflist)); |
| 65 | } |
| 66 | |
| 67 | if (py::isinstance<py::tuple>(val)) |
| 68 | { |
| 69 | auto et = py::cast<py::tuple>(val); |
| 70 | Array<shared_ptr<CoefficientFunction>> cflist(et.size()); |
| 71 | for (int i : Range(cflist.Size())) |
| 72 | cflist[i] = MakeCoefficient(et[i]); |
| 73 | return MakeVectorialCoefficientFunction(std::move(cflist)); |
| 74 | } |
| 75 | // throw std::invalid_argument(string("Cannot make CoefficientFunction from ") + string(py::str(val)) + " of type " + string(py::str(val.get_type()))); |
| 76 | throw std::invalid_argument(string("Cannot make CoefficientFunction from ") + string(py::str(val)) + " of type " + string(py::str(py::type::of(val)))); |
| 77 | } |
| 78 | |
| 79 | Array<shared_ptr<CoefficientFunction>> MakeCoefficients (py::object py_coef) |
| 80 | { |
no test coverage detected