def diff(v): """Calculate approximate derivative. Calculate approximate derivative from v as d = [v_1-v_0, v2-v_1, ...] Parameters ---------- v : array(N) | pg.R3Vector(N) Array of double values or positions Returns ------- d : [type(v)](N-1) | derivative array Examples -------- >>> import pygimli as pg >>> from pygimli.utils import diff >>> p = pg.R3Vector(4) >>> p[0] = [0.0, 0.0] >>> p[1] = [0.0, 1.0] >>> print(diff(p)[0]) RVector3: (0.0, 1.0, 0.0) >>> print(diff(p)[1]) RVector3: (0.0, -1.0, 0.0) >>> print(diff(p)[2]) RVector3: (0.0, 0.0, 0.0) >>> p = pg.RVector(3) >>> p[0] = 0.0 >>> p[1] = 1.0 >>> p[2] = 2.0 >>> print(diff(p)) 2 [1.0, 1.0] """ d = None if isinstance(v, np.ndarray): if v.ndim == 2: if v.shape[1] < 4: # v = pg.R3Vector(v.T) vt = v.copy() v = pg.R3Vector(len(vt)) for i, vi in enumerate(vt): v.setVal(pg.RVector3(vi), i) else: v = pg.R3Vector(v) else: v = pg.RVector(v) elif isinstance(v, list): v = pg.R3Vector(v) if isinstance(v, pg.R3Vector) or isinstance(v, pg.stdVectorRVector3): d = pg.R3Vector(len(v) - 1) else: d = pg.RVector(len(v) - 1) for i, _ in enumerate(d): d[i] = v[i + 1] - v[i] return d
def test_ListToR3Vector(self): """ implemented in custom_rvalue.cpp""" x = [0.0, 1.0, 0.0] p = pg.RVector3(x) pl = [p, p, p] t = pg.R3Vector(pl) self.assertEqual(t.size(), len(pl))
def test_NumpyToScalar(self): """Implemented through automatic iterator """ x = pg.RVector(2) x3 = pg.R3Vector(2) w = pg.RVector() x += np.float32(1.0) np.testing.assert_equal(sum(x + 1.0), 4.0) np.testing.assert_equal(sum(x + np.float32(1)), 4.0) np.testing.assert_equal(sum(x + np.float64(1)), 4.0) np.testing.assert_equal(sum(x - 1.0), 0.0) np.testing.assert_equal(sum(x - np.float32(1)), 0.0) np.testing.assert_equal(sum(x - np.float64(1)), 0.0) # HarmonicModelling(size_t nh, const RVector & tvec); pg.HarmonicModelling(np.int32(1), x) pg.HarmonicModelling(np.uint32(1), x) pg.HarmonicModelling(np.int64(1), x) pg.HarmonicModelling(np.uint64(1), x) # pg.PolynomialModelling(1, np.int32(1), x3, x); # pg.PolynomialModelling(1, np.int64(1), x3, x); # pg.PolynomialModelling(1, np.uint32(1), x3, x); # pg.PolynomialModelling(1, np.uint64(1), x3, x); x = pg.Pos(0.0, 0.0, 0.0) x += np.float32(1) np.testing.assert_equal(x, pg.Pos(1.0, 1.0, 1.0)) np.testing.assert_equal(x - 1, pg.Pos(0.0, 0.0, 0.0)) np.testing.assert_equal(x - np.float32(1), pg.Pos(0.0, 0.0, 0.0)) np.testing.assert_equal(x - np.float64(1), pg.Pos(0.0, 0.0, 0.0))
def test_ListToR3Vector(self): ''' custom_rvalue.cpp ''' x = [0.0, 1.0, 0.0] p = pg.RVector3(x) pl = [p, p, p] t = pg.R3Vector(pl) self.assertEqual(t.size(), len(pl))
def cellDataToBoundaryData(mesh, data): """ TODO DOCUMENT_ME """ if len(data) != mesh.cellCount(): raise BaseException( "Dimension mismatch, expecting cellCount(): " + str(mesh.cellCount()) + "got: " + str(len(data)), str(len(data[0]))) CtB = mesh.cellToBoundaryInterpolation() if isinstance(data, pg.R3Vector()): return np.array([CtB * pg.x(data), CtB * pg.y(data), CtB * pg.z(data)]).T else: return CtB * data
def test_R3VectorIndex(self): r3 = pg.R3Vector(10) self.assertEqual(r3[0], pg.RVector3(0)) np.testing.assert_array_equal(r3[0], pg.RVector3(0)) r3[1] = pg.RVector3(0.0, 1.0, 0.0) np.testing.assert_array_equal(r3[1], pg.RVector3(0.0, 1.0, 0.0)) r3[2] = (0.0, 2.0, 0.0) np.testing.assert_array_equal(r3[2], pg.RVector3(0.0, 2.0, 0.0)) r3[3] = (0.0, 3.0, 0.0) np.testing.assert_array_equal(r3[3], pg.RVector3(0.0, 3.0, 0.0)) d = pg.utils.dist(r3) self.assertEqual(sum(d), 1 + 2 + 3)