basic_arithmetics.py 2.42 KB
 Theo Steininger committed Apr 13, 2017 1 2 3 4 5 6 7 8 9 10 11 12 ``````# This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . `````` Theo Steininger committed May 24, 2017 13 14 15 16 17 ``````# # Copyright(C) 2013-2017 Max-Planck-Society # # NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik # and financially supported by the Studienstiftung des deutschen Volkes. `````` Theo Steininger committed Oct 13, 2016 18 19 20 21 22 23 24 25 26 27 28 `````` import numpy as np from d2o import distributed_data_object from nifty.field import Field __all__ = ['cos', 'sin', 'cosh', 'sinh', 'tan', 'tanh', 'arccos', 'arcsin', 'arccosh', 'arcsinh', 'arctan', 'arctanh', 'sqrt', 'exp', 'log', 'conjugate'] `````` Martin Reinecke committed Apr 20, 2017 29 ``````def _math_helper(x, function): `````` Theo Steininger committed Oct 13, 2016 30 `````` if isinstance(x, Field): `````` Martin Reinecke committed Apr 20, 2017 31 32 33 `````` result_val = x.val.apply_scalar_function(function) result = x.copy_empty(dtype=result_val.dtype) result.val = result_val `````` Theo Steininger committed Oct 13, 2016 34 `````` elif isinstance(x, distributed_data_object): `````` Martin Reinecke committed Apr 20, 2017 35 `````` result = x.apply_scalar_function(function, inplace=False) `````` Theo Steininger committed Oct 13, 2016 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 `````` else: result = function(np.asarray(x)) return result def cos(x): return _math_helper(x, np.cos) def sin(x): return _math_helper(x, np.sin) def cosh(x): return _math_helper(x, np.cosh) def sinh(x): return _math_helper(x, np.sinh) def tan(x): return _math_helper(x, np.tan) def tanh(x): return _math_helper(x, np.tanh) def arccos(x): return _math_helper(x, np.arccos) def arcsin(x): return _math_helper(x, np.arcsin) def arccosh(x): return _math_helper(x, np.arccosh) def arcsinh(x): return _math_helper(x, np.arcsinh) def arctan(x): return _math_helper(x, np.arctan) def arctanh(x): return _math_helper(x, np.arctanh) def sqrt(x): return _math_helper(x, np.sqrt) def exp(x): return _math_helper(x, np.exp) def log(x, base=None): result = _math_helper(x, np.log) if base is not None: result = result/log(base) return result def conjugate(x): return _math_helper(x, np.conjugate) def conj(x): return _math_helper(x, np.conjugate)``````