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

Last change on this file since 14310 was 14310, checked in by Mathieu Morlighem, 12 years ago

merged trunk-jpl and trunk for revision 14308

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