File: utils.py

package info (click to toggle)
xraylib 4.0.0%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 46,936 kB
  • sloc: ansic: 16,103; f90: 8,746; java: 6,766; python: 1,497; cpp: 1,305; pascal: 1,139; makefile: 809; ruby: 622; php: 594; perl: 573; cs: 193; sh: 125
file content (35 lines) | stat: -rw-r--r-- 899 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from __future__ import print_function
import numpy as np

def cshift(arr, offset):
	return np.roll(arr, offset)

def deriv(x, y):
	if len(x.shape) != 1 or x.shape[0] != y.shape[0]:
		raise TypeError("deriv: x and y must be 1D numpy arrays of the same length")

	x0 = cshift(x, 1)
	x2 = cshift(x, -1)
	x01 = x0 - x
	x02 = x0 - x2
	x12 = x - x2

	d = cshift(y, 1) * (x12 / (x01 * x02)) + \
		y * (1.0/x12 - 1.0/x01) - \
		cshift(y, -1) * (x01 / (x02 * x12))

	d[0] = y[0] * (x01[1]+x02[1])/(x01[1]*x02[1]) - \
		y[1] * x02[1]/(x01[1]*x12[1]) + \
		y[2] * x01[1]/(x02[1]*x12[1])

	d[-1] = -y[-3] * x12[-2]/(x01[-2]*x02[-2]) + \
		y[-2] * x02[-2]/(x01[-2]*x12[-2]) - \
		y[-1] * (x02[-2]+x12[-2]) / (x02[-2]*x12[-2])
	return d

if __name__ == "__main__":
	x = np.linspace(0.0, 10.0, 1001, dtype=np.float64)
	y = np.sin(x)
	dy = deriv(x, y)
	print("max diff {}".format(np.max(np.abs(dy - np.cos(x)))))