예제 #1
0
	def Keys( self ) : 
		'''
		return:
			all keys in a
		'''
		from jizhipy.Basic import IsType
		keys = self.classinstance.__dict__.keys()
		dowhile, times = True, 0
		while(dowhile) : 
			dowhile, times = False, times+1
			for i in range(len(keys)) : 
				k = keys[i].split('.')
				v = self.classinstance
				for j in range(len(k)) : v = v.__dict__[k[j]]
				if (IsType.isfunc(v) or IsType.isclass(v)) : 
					keys[i] = ''
				elif (IsType.isinstance(v) and not IsType.isspecialinstance(v)) : 
					k = v.__dict__.keys()
					for j in range(len(k)) : k[j] = keys[i] + '.'+k[j]
					keys[i] = k
					dowhile = True
			keytmp = []
			for i in range(len(keys)) : 
				if (keys[i] == '') : continue
				elif (type(keys[i])==list): keytmp += keys[i]
				else : keytmp.append(keys[i])
			keys = keytmp
		self.keys = keys
예제 #2
0
    def RandomVariable(self, shape, x, pdf, norm=True):
        '''
		Invert operation of ProbabilityDensity()
		Provide probability density, return random variable

		shape:
			The shape of generated random variable
	
		pdf==fx, norm:
			fx: 
				isfunc | isndarray
				(1) isfunc: fx = def f(x), f(x) is the probability density function
				(2) isndarray: fx.size = x.size
			norm:
				True | False
				fx must be
					1. fx >= 0
					2. \int_{-\inf}^{+\inf} fx dx = 1
				Only if norm=False, not normal it, otherwise, always normal it.
	
		x:
			isndarray, must be 1D
			Use fx and x to obtain the inverse function of the cumulative distribution function, x = F^{-1}(y)
	
		return: 
			1D ndarray with shape, random variable
		'''
        import numpy as np
        from jizhipy.Array import Asarray
        from jizhipy.Basic import IsType, Raise
        from jizhipy.Optimize import Interp1d
        #---------------------------------------------
        x = Asarray(x).flatten()
        if (not IsType.isfunc(fx)):
            fx = Asarray(fx).flatten()
            if (x.size != fx.size):
                Raise(Exception,
                      'fx.size=' + str(fx.size) + ' != x.size=' + str(x.size))
        else:
            fx = fx(x)
        fx *= 57533.4
        #---------------------------------------------
        # sort x from small to large
        x = np.sort(x + 1j * fx)
        fx, x = x.imag, x.real
        #---------------------------------------------
        dx = x[1:] - x[:-1]
        dx = np.append(dx, dx[-1:])
        #---------------------------------------------
        # Normal fx
        if (norm is not False):
            fxmin = fx.min()
            if (fxmin < 0): fx -= fxmin
            fx /= (fx.sum() * dx)
        #---------------------------------------------
        # Cumulative distribution function
        fx = fx.cumsum() * dx
        #---------------------------------------------
        # Inverse function
        F_1 = Interp1d(fx, x, None)
        #---------------------------------------------
        # Uniform random with shape
        x = np.random.random(shape)
        #---------------------------------------------
        # Random variable with f(x)
        b = F_1(x)
        return b
예제 #3
0
def Quad(func, ranges, args=(), err=False):
    '''
	integrate

	func:
		<type 'function'>
		def func(x, y, z, a, b, c) : 
			return (a*x + b*y**2) * z + c

	ranges:
		* Must list | tuple
		* len(ranges) == number of variables
		* ranges[i]:
			Must list | tuple and must len(ranges[i]) == 2
		* ranges = [first, second, third, ......] parameter of func from left to right
			Here first-x, second-y, third-z

	args:
		Must tuple, if len==1: args=(a,) => don't forget `,`
		arguments of the constants in func, from left to right
		Here args=(a,b,c)

	err:
		True | False
		Return the error the result or not?
	'''
    from scipy.integrate import dblquad
    from scipy.integrate import tplquad
    from scipy.integrate import nquad
    from jizhipy.Basic import IsType, Raise
    constranges = True
    for i in range(len(ranges)):
        if (IsType.isfunc(ranges[i][0])): constranges = False
        if (IsType.isfunc(ranges[i][1])): constranges = False
        if (not constranges): break
    #--------------------------------------------------
    if (constranges):
        res = nquad(func, ranges, args)
        #--------------------------------------------------
    elif (len(ranges) == 2):
        x1, x2 = ranges[0]
        if (not IsType.isfunc(x1)):

            def xlow(y):
                return x1
        else:

            xlow = x1
        if (not IsType.isfunc(x2)):

            def xup(y):
                return x2
        else:

            xup = x2
        res = dblquad(func, ranges[1][0], ranges[1][1], xlow, xup, args)
    #--------------------------------------------------
    elif (len(ranges) == 3):
        y1, y2 = ranges[1]
        if (not IsType.isfunc(y1)):

            def ylow(z):
                return y1
        else:

            ylow = y1
        if (not IsType.isfunc(y2)):

            def yup(z):
                return y2
        else:

            yup = y2
        #--------------------
        x1, x2 = ranges[0]
        if (not IsType.isfunc(x1)):

            def xlow(y, z):
                return x1
        else:

            xlow = x1
        if (not IsType.isfunc(x2)):

            def xup(y, z):
                return x2
        else:

            xup = x2
        res = tplquad(func, ranges[2][0], ranges[2][1], ylow, yup, xlow, xup)
    else:
        Raise(
            Exception, 'constranges = False and len(ranges) = ' +
            str(len(ranges)) + ' > 3')
    #--------------------------------------------------
    if (err): return res
    else: return res[0]