File: testMathHelper.py

package info (click to toggle)
yade 2026.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,448 kB
  • sloc: cpp: 97,645; python: 52,173; sh: 677; makefile: 162
file content (152 lines) | stat: -rw-r--r-- 4,055 bytes parent folder | download | duplicates (3)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/python
# -*- coding: utf-8 -*-
##########################################################################
#  2019        Janek Kozicki                                             #
#                                                                        #
#  This program is free software; it is licensed under the terms of the  #
#  GNU General Public License v2 or later. See file LICENSE for details. #
##########################################################################

# When mpmath is not required implement a super-minimal version of mpmath, so that the tests will work and pretend that they use mpmath
# So this file is in fact to avoid python3-mpmath dependency when high-precision is not used.

import yade

# Without mpmath use a super-minimal version of mpmath, so that all the tests will work and pretend that they use mpmath
print('\n\033[92m' + "Using " + yade.config.highPrecisionName + " with " + str(yade.config.highPrecisionDecimalPlaces) + " digits." + '\033[0m')
#print('\033[92m'+"Will not 'import mpmath' to reduce dependency on mpmath. Using a local minimal replacement module instead."+'\033[0m\n')
from math import *
import math
import cmath


class MP:
	dps = None


mp = MP
mpf = float


# simulate names provided by Eigen NumTraits
def eps():
	return 0


def cbrt(a):
	return pow(a, 1. / 3.)


power = pow
euler = 0.57721566490153286060651209008240243104
catalan = 0.9159655941772190150546035149323841107


# mpc needs to accept two string arguments, apart from that it's just a complex number
class mpc(complex):

	def __new__(cls, *args, **kwargs):
		super_new = super(mpc, cls).__new__
		if super_new is object.__new__:
			return super_new(cls)
		if len(args) == 2:
			return super_new(cls, float(args[0]), float(args[1]))
		return super_new(cls, *args, **kwargs)


# simulate all complex functions, just like they work in mpmath
# these overrides serve only one purpose: so that math functions can have the same name for complex and real numbers.
# They have the same names as in mpmath
def conj(a):
	return complex(a).conjugate()


def re(a):
	return complex(a).real


def im(a):
	return complex(a).imag


def norm(a):
	return abs(complex(a))  # // Warning: C++ std::norm is squared length. In python/mpmath norm is C++ abs == length.


def phase(a):
	return cmath.phase(a)  # tests std::arg


# skip squaredNorm, for    testing std::norm I will use norm(a)*norm(a) here.
# skip proj       , I skip testing std::proj. Later maybe add, see https://en.cppreference.com/w/cpp/numeric/complex/proj is checking for infinities in either component.
def rect(a, b):
	return cmath.rect(a, b)  # tests std::polar


def sin(a):
	return (cmath.sin(a) if (a.__class__ == mpc) else math.sin(a))


def sinh(a):
	return (cmath.sinh(a) if (a.__class__ == mpc) else math.sinh(a))


def cos(a):
	return (cmath.cos(a) if (a.__class__ == mpc) else math.cos(a))


def cosh(a):
	return (cmath.cosh(a) if (a.__class__ == mpc) else math.cosh(a))


def tan(a):
	return (cmath.tan(a) if (a.__class__ == mpc) else math.tan(a))


def tanh(a):
	return (cmath.tanh(a) if (a.__class__ == mpc) else math.tanh(a))


def asin(a):
	return (cmath.asin(a) if (a.__class__ == mpc) else math.asin(a))


def asinh(a):
	return (cmath.asinh(a) if (a.__class__ == mpc) else math.asinh(a))


def acos(a):
	return (cmath.acos(a) if (a.__class__ == mpc) else math.acos(a))


def acosh(a):
	return (cmath.acosh(a) if (a.__class__ == mpc) else math.acosh(a))


def atan(a):
	return (cmath.atan(a) if (a.__class__ == mpc) else math.atan(a))


def atanh(a):
	return (cmath.atanh(a) if (a.__class__ == mpc) else math.atanh(a))


def exp(a):
	return (cmath.exp(a) if (a.__class__ == mpc) else math.exp(a))


def log(a):
	return (cmath.log(a) if (a.__class__ == mpc) else math.log(a))


def log10(a):
	return (cmath.log10(a) if (a.__class__ == mpc) else math.log10(a))


# skip pow        , for    testing std::pow I will use a**b
def sqrt(a):
	return (cmath.sqrt(a) if (a.__class__ == mpc) else math.sqrt(a))


def factorial(a):
	return math.factorial(a)