MCPcopy Create free account
hub / github.com/NGSolve/ngsolve / PyVecAccess

Function PyVecAccess

basiclinalg/python_bla.cpp:65–117  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

63
64template <typename T, typename TNEW = T, typename TCLASS = py::class_<T> >
65void PyVecAccess( py::module &m, TCLASS &c )
66{
67 typedef typename T::TSCAL TSCAL;
68 c.def("__getitem__", [](T &self, py::slice inds )-> TNEW {
69 size_t start, step, n;
70 InitSlice( inds, self.Size(), start, step, n );
71 TNEW res(n);
72 for (int i=0; i<n; i++, start+=step)
73 res[i] = self[start];
74 return res;
75 }, py::arg("inds"), "Return values at given positions" );
76 c.def("__getitem__", [](T &v, py::list ind )-> TNEW {
77 int n = py::len(ind);
78 TNEW res(n);
79 for (int i=0; i<n; i++) {
80 res[i] = v[ ind[i].cast<int>() ];
81 }
82 return res;
83 }, py::arg("ind"), "Return values at given positions" );
84 c.def("__setitem__", [](T &self, py::slice inds, const T & rv ) {
85 size_t start, step, n;
86 InitSlice( inds, self.Size(), start, step, n );
87 for (int i=0; i<n; i++, start+=step)
88 self[start] = rv[i];
89 }, py::arg("inds"), py::arg("rv"), "Set values at given positions" );
90 c.def("__setitem__", [](T &self, py::slice inds, TSCAL val ) {
91 size_t start, step, n;
92 InitSlice( inds, self.Size(), start, step, n );
93 for (int i=0; i<n; i++, start+=step)
94 self[start] = val;
95 }, py::arg("inds"), py::arg("value"), "Set value at given positions" );
96 // c.def("__setitem__", [](T &self, py::slice inds, const std::vector<TSCAL> & vec ) {
97 c.def("__setitem__", [](T &self, py::slice inds, py::array_t<TSCAL> bvec ) {
98 auto vec = bvec. template unchecked<1>();
99 size_t start, step, n;
100 InitSlice( inds, self.Size(), start, step, n );
101 for (int i=0; i<n; i++, start+=step)
102 self[start] = vec(i);
103 }, py::arg("inds"), py::arg("value"), "Set value at given positions" );
104 c.def("__add__" , [](T &self, T &v) { return TNEW(self+v); }, py::arg("vec") );
105 c.def("__sub__" , [](T &self, T &v) { return TNEW(self-v); }, py::arg("vec") );
106 c.def("__mul__" , [](T &self, TSCAL s) { return TNEW(s*self); }, py::arg("value") );
107 c.def("__rmul__" , [](T &self, TSCAL s) { return TNEW(s*self); }, py::arg("value") );
108 c.def("__neg__" , [](T &self) { return TNEW(-self); });
109 c.def("InnerProduct", [](T & x, T & y, bool conjugate)
110 {
111 if (conjugate)
112 return InnerProduct (x, Conj(y));
113 else
114 return InnerProduct (x, y);
115 }, py::arg("y"), py::arg("conjugate")=true, "Returns InnerProduct with other object");
116 c.def("Norm", [](T & x) { return L2Norm(x); }, "Returns L2-norm");
117}
118
119
120

Callers

nothing calls this directly

Calls 6

InitSliceFunction · 0.85
argFunction · 0.85
InnerProductFunction · 0.70
ConjFunction · 0.70
L2NormFunction · 0.70
SizeMethod · 0.45

Tested by

no test coverage detected