File: maths.py

package info (click to toggle)
metview-python 1.16.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,900 kB
  • sloc: python: 11,306; makefile: 84; ansic: 51; sh: 7
file content (168 lines) | stat: -rw-r--r-- 2,106 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# (C) Copyright 2017- ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
#
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.
import numpy as np


def neg(x):
    return -x


def pos(x):
    return x


# def abs(x):
#     return np.abs(x)


def not_func(x):
    return (~(x != 0)).astype(int)


def add(x, y):
    return x + y


def sub(x, y):
    return x - y


def mul(x, y):
    return x * y


def div(x, y):
    return x / y


def pow(x, y):
    return x**y


def ge(x, y):
    return (x >= y).astype(int)


def gt(x, y):
    return (x > y).astype(int)


def le(x, y):
    return (x <= y).astype(int)


def lt(x, y):
    return (x < y).astype(int)


def eq(x, y):
    return (x == y).astype(int)


def ne(x, y):
    return (x != y).astype(int)


def and_func(x, y):
    return ne(x, 0) * ne(y, 0)


def or_func(x, y):
    return np.clip(ne(x, 0) + ne(y, 0), 0, 1)


def set_from_other(x, y):
    return y


# single argument functions


def abs(x):
    return np.fabs(x)


def acos(x):
    return np.arccos(x)


def asin(x):
    return np.arcsin(x)


def atan(x):
    return np.arctan(x)


def cos(x):
    return np.cos(x)


def exp(x):
    return np.exp(x)


def log(x):
    return np.log(x)


def log10(x):
    return np.log10(x)


def sgn(x):
    return np.sign(x)


def square(x):
    return np.square(x)


def sqrt(x):
    return np.sqrt(x)


def sin(x):
    return np.sin(x)


def tan(x):
    return np.tan(x)


# double argument functions


def atan2(x, y):
    return np.arctan2(x, y)


def floor_div(x, y):
    return np.floor_divide(x, y)


def mod(x, y):
    return np.mod(x, y)


# bitmapping


def bitmap(x, y):
    if isinstance(y, (int, float)):
        x[x == y] = np.nan
        return x
    elif isinstance(y, np.ndarray):
        x[np.isnan(y)] = np.nan
        return x


def nobitmap(x, y):
    x[np.isnan(x)] = y
    return x