source: issm/trunk/src/m/miscellaneous/MatlabFuncs.py@ 24313

Last change on this file since 24313 was 24313, checked in by Mathieu Morlighem, 5 years ago

merged trunk-jpl and trunk for revision 24310

File size: 2.0 KB
Line 
1def oshostname():
2 import socket
3
4 return socket.gethostname()
5
6
7def ispc():
8 import platform
9
10 if 'Windows' in platform.system():
11 return True
12 else:
13 return False
14
15
16def ismac():
17 import platform
18
19 if 'Darwin' in platform.system():
20 return True
21 else:
22 return False
23
24
25def strcmp(s1, s2):
26
27 if s1 == s2:
28 return True
29 else:
30 return False
31
32
33def strncmp(s1, s2, n):
34
35 if s1[0:n] == s2[0:n]:
36 return True
37 else:
38 return False
39
40
41def strcmpi(s1, s2):
42
43 if s1.lower() == s2.lower():
44 return True
45 else:
46 return False
47
48
49def strncmpi(s1, s2, n):
50
51 if s1.lower()[0:n] == s2.lower()[0:n]:
52 return True
53 else:
54 return False
55
56
57def ismember(a, s):
58 import numpy as np
59
60 if not isinstance(s, (tuple, list, dict, np.ndarray)):
61 s = [s]
62
63 if not isinstance(a, (tuple, list, dict, np.ndarray)):
64 a = [a]
65
66 if not isinstance(a, np.ndarray):
67 b = [item in s for item in a]
68
69 else:
70 if not isinstance(s, np.ndarray):
71 b = np.empty_like(a)
72 for i, item in enumerate(a.flat):
73 b.flat[i] = item in s
74 else:
75 b = np.in1d(a.flat, s.flat).reshape(a.shape)
76
77 return b
78
79
80def det(a):
81
82 if a.shape == (1, ):
83 return a[0]
84 elif a.shape == (1, 1):
85 return a[0, 0]
86 elif a.shape == (2, 2):
87 return a[0, 0] * a[1, 1] - a[0, 1] * a[1, 0]
88 else:
89 raise TypeError("MatlabFunc.det only implemented for shape (2, 2), not for shape %s." % str(a.shape))
90
91
92def sparse(ivec, jvec, svec, m=0, n=0, nzmax=0):
93 import numpy as np
94
95 if not m:
96 m = np.max(ivec)
97 if not n:
98 n = np.max(jvec)
99
100 a = np.zeros((m, n))
101
102 for i, j, s in zip(ivec.reshape(-1, order='F'), jvec.reshape(-1, order='F'), svec.reshape(-1, order='F')):
103 a[i - 1, j - 1] += s
104
105 return a
106
107
108def heaviside(x):
109 import numpy as np
110
111 y = np.zeros_like(x)
112 y[np.nonzero(x > 0.)] = 1.
113 y[np.nonzero(x == 0.)] = 0.5
114
115 return y
Note: See TracBrowser for help on using the repository browser.