Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
victorlei committed Oct 19, 2014
1 parent cbee924 commit 4f23702
Showing 1 changed file with 73 additions and 1 deletion.
74 changes: 73 additions & 1 deletion smop/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,20 @@
"""
import version
import math
from numpy import inf

from numpy.linalg import inv as _inv
from numpy.linalg import eig as _eig
from numpy.linalg import qr as _qr
try:
from scipy.linalg import schur as _schur
except ImportError:
pass

import numpy as np
import os,sys,copy
import os,sys,copy,time
from sys import stdin,stdout,stderr
try:
from scipy.io import loadmat
except:
Expand Down Expand Up @@ -424,6 +435,10 @@ def copy(a):
def disp(*args):
print (args)

def eig(a):
u,v = _eig(np.asarray(a))
return matlabarray(u).T

def exist(a,b):
if str(b) == 'builtin':
return str(a) in globals()
Expand All @@ -436,6 +451,9 @@ def false(*args):
args += args
return np.zeros(args,dtype=bool,order="F")

def fft2(a):
return np.fft.fft2(a)

def find(a,n=None,d=None,nargout=1):
if d:
raise NotImplementedError
Expand Down Expand Up @@ -469,6 +487,12 @@ def fopen(*args):
except:
return -1

def fflush(fp):
fp.flush()

def fprintf(fp,fmt,*args):
print str(fmt) % tuple(str(a) if isinstance(a,char) else a for a in args)

def fullfile(*args):
return os.path.join(*args)

Expand All @@ -485,6 +509,9 @@ def intersect(a,b,nargout=1):
return np.array(c)
raise NotImplementedError

def inv(a):
return matlabarray(_inv(np.asarray(a)))

#def inf(*args):
# t = np.empty(np.prod(args))
# t[:] = np.inf
Expand Down Expand Up @@ -518,6 +545,9 @@ def isequal(a,b):
return np.array_equal(np.asanyarray(a),
np.asanyarray(b))

def isfield(a,b):
return str(b) in a.__dict__.keys()

def isscalar(a):
"""np.isscalar returns True if a.__class__ is a scalar
type (i.e., int, and also immutable containers str and
Expand Down Expand Up @@ -567,6 +597,16 @@ def ones(*args,**kwargs):
args += args
return matlabarray(np.ones(args,order="F",**kwargs))

def primes(upto):
primes=np.arange(2,upto+1)
isprime=np.ones(upto-1,dtype=bool)
for factor in primes[:int(math.sqrt(upto))]:
if isprime[factor-2]: isprime[factor*2-2::factor]=0
return primes[isprime]

def qr(a):
return matlabarray(_qr(np.asarray(a)))

def rand(*args,**kwargs):
if not args:
return np.random.rand()
Expand All @@ -577,15 +617,41 @@ def rand(*args,**kwargs):
except:
pass

def rand(*args,**kwargs):
if not args:
return np.random.rand()
if len(args) == 1:
args += args
try:
return np.random.rand(np.prod(args)).reshape(args,order="F")
except:
pass

def randn(*args,**kwargs):
if not args:
return np.random.randn()
if len(args) == 1:
args += args
try:
return np.random.randn(np.prod(args)).reshape(args,order="F")
except:
pass

def ravel(a):
return np.asanyarray(a).reshape(-1,1)

def roots(a):
return matlabarray(np.roots(np.asarray(a).ravel()))

def round(a):
return np.round(np.asanyarray(a))

def rows(a):
return np.asarray(a).shape[0]

def schur(a):
return matlabarray(_schur(np.asarray(a)))

def size(a, b=0, nargout=1):
"""
>>> size(zeros(3,3)) + 1
Expand Down Expand Up @@ -623,6 +689,12 @@ def toupper(a):

true = True

def tic():
return time.clock()

def toc(t):
return time.clock()-t

def true(*args):
if len(args) == 1:
args += args
Expand Down

0 comments on commit 4f23702

Please sign in to comment.