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

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

merged trunk-jpl and trunk for revision 13974

File size: 1.5 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 strcmp(s1,s2):
15
16 if s1 == s2:
17 return True
18 else:
19 return False
20
21def strncmp(s1,s2,n):
22
23 if s1[0:n] == s2[0:n]:
24 return True
25 else:
26 return False
27
28def strcmpi(s1,s2):
29
30 if s1.lower() == s2.lower():
31 return True
32 else:
33 return False
34
35def strncmpi(s1,s2,n):
36
37 if s1.lower()[0:n] == s2.lower()[0:n]:
38 return True
39 else:
40 return False
41
42def ismember(a,s):
43 import numpy
44
45 if not isinstance(s,(tuple,list,dict,numpy.ndarray)):
46 s=[s]
47
48 if not isinstance(a,(tuple,list,dict,numpy.ndarray)):
49 a=[a]
50
51 if not isinstance(a,numpy.ndarray):
52 b=[item in s for item in a]
53
54 else:
55 if not isinstance(s,numpy.ndarray):
56 b=numpy.empty_like(a)
57 for i,item in enumerate(a.flat):
58 b.flat[i]=item in s
59 else:
60 b=numpy.in1d(a.flat,s.flat).reshape(a.shape)
61
62 return b
63
64def det(a):
65 import numpy
66
67 if a.shape==(1,):
68 return a[0]
69 elif a.shape==(1,1):
70 return a[0,0]
71 elif a.shape==(2,2):
72 return a[0,0]*a[1,1]-a[0,1]*a[1,0]
73 else:
74 raise TypeError("MatlabFunc.det only implemented for shape (2, 2), not for shape %s." % str(a.shape))
75
76def sparse(ivec,jvec,svec,m=0,n=0,nzmax=0):
77 import numpy
78
79 if not m:
80 m=numpy.max(ivec)
81 if not n:
82 n=numpy.max(jvec)
83
84 a=numpy.zeros((m,n))
85
86 for i,j,s in zip(ivec.reshape(-1,order='F'),jvec.reshape(-1,order='F'),svec.reshape(-1,order='F')):
87 a[i-1,j-1]+=s
88
89 return a
90
Note: See TracBrowser for help on using the repository browser.