コード例 #1
0
def LiX(num):

    function = lambda t: (1 / math.log(t))
    res = 0

    #Special case: integration leads to 0 in denominator
    if (num == 2):
        return 1

    #Split into components for more precise integration for num > 1000
    if (num > 1000 and int(num / 1000) > 1):
        mod = num % 1000
        div = int(num / 1000)

        #Add integration over 1000s
        for i in range(1, div + 1, 1):
            res += quadpack.quad(function, (i - 1) * 1000, i * 1000)[0]

        #Add remainder
        res += quadpack.quad(function, div * 1000, div * 1000 + mod)[0]

    #Just calculate it directly
    else:
        res += quadpack.quad(function, 0, num)[0]

    return math.floor(res)
コード例 #2
0
def main(x0, y0, x1, y1, x2, y2):
    arr = [[0, 0], [0, 1], [1, 0]]

    pts = []
    for e, n in arr:
        pnt = generalT(e, n)
        pts.append([pnt[0][0], pnt[1][0]])

    global det
    det = np.linalg.det(matrix)

    res1 = 0
    res2 = 0
    res3 = 0

    lagrange = calcLagrange((x0, y0), (x1, y1), (x2, y2))

    for i in range(3):
        alpha, err = quad(outerIntegral, a=0, b=1, args=(i, lagrange))

        print("alpha #" + str(i) + ": " + '{:.2f}'.format(alpha))

        res1 += alpha * fxy1(pts[i][0], pts[i][1])
        res2 += alpha * fxy2(pts[i][0], pts[i][1])
        res3 += alpha * fxy3(pts[i][0], pts[i][1])
        print()

    print("Ergebnis0: " + '{:.4f}'.format(res1))
    print("Ergebnis1: " + '{:.4f}'.format(res2))
    print("Ergebnis2: " + '{:.4f}'.format(res3))
    return res2  #für g
コード例 #3
0
def compute_tau_z(Q, zmin=0.6, zmax=14., dz=0.3):
    z_integrate = arange(zmin, zmax, dz)
    taus_plot = []
    for i in z_integrate:
        taus_plot.append(quadpack.quad(tau, 0.3, i)[0])

    return z_integrate, taus_plot
コード例 #4
0
    def test1DNormalDist(self):
        # prepare data
        U = dists.Normal(1.85, .3, 0, 3)
        trainSamples = np.array([U.rvs(500)]).T
        testSamples = np.array([U.rvs(1000)]).T

        # build parameter set
        dist = KDEDist(trainSamples,
                       kernelType=KernelType_GAUSSIAN,
                       bandwidthOptimizationType=
                       BandwidthOptimizationType_MAXIMUMLIKELIHOOD,
                       bounds=U.getBounds())

        #         fig = plt.figure()
        #         plotDensity1d(U)
        #         plotDensity1d(dist)

        print("quad = %s" % (quad(lambda x: dist.pdf([x]), 0, 3), ))
        print("mean = %g ~ %g" % (U.mean(), dist.mean()))
        print("var = %g ~ %g" % (U.var(), dist.var()))
        print("KL = %g" % U.klDivergence(dist, testSamples, testSamples))
        print("CE = %g" % dist.crossEntropy(testSamples))
        print("MSE = %g" % dist.l2error(U, testSamples, testSamples))

        plt.show()
コード例 #5
0
def calc_area(f_x_sym, f_y_sym, t_val):
    from sympy.abc import t

    f_x = lambdify(t, f_x_sym,['numpy'])
    f_y = lambdify(t, f_y_sym, ['numpy'])
    df_x = lambdify(t, diff(f_x_sym), ['numpy'])
    df_y = lambdify(t, diff(f_y_sym), ['numpy'])

    integrand = lambda tau: f_x(tau)*df_y(tau)-df_x(tau)*f_y(tau)
    return abs(0.5 * quad(integrand, 0, t_val)[0])
コード例 #6
0
def calc_area(f_x_sym, f_y_sym, t_val):
    from sympy.abc import t

    f_x = lambdify(t, f_x_sym, ['numpy'])
    f_y = lambdify(t, f_y_sym, ['numpy'])
    df_x = lambdify(t, diff(f_x_sym), ['numpy'])
    df_y = lambdify(t, diff(f_y_sym), ['numpy'])

    integrand = lambda tau: f_x(tau) * df_y(tau) - df_x(tau) * f_y(tau)
    return abs(0.5 * quad(integrand, 0, t_val)[0])
コード例 #7
0
ファイル: problem2.py プロジェクト: dacsgb/Comp-Methods
def secondOrderSys(C, t_span):
    t_deriv = np.zeros(1)
    for i in range(t_span[1]):
        sol = opt.fsolve(deriv, i, args=(C))
        t_deriv = np.append(t_deriv, sol[0])
    sys_t_deriv = sys(t_deriv, C)
    sys_max = np.max(sys_t_deriv)
    sys_min = np.min(sys_t_deriv)
    if abs(sys_max) > abs(sys_min):
        t_sol = sys_max
    else:
        t_sol = sys_min

    err = quad.quad(integral, t_span[0], t_span[1], args=(C))
    print('Peak location: t = {:.3f}'.format(t_sol))
    print('Peak value: x = {:.3f}'.format(sys(t_sol, C)))
    print('Error integral: err = {:.3f}'.format(err[0]))
コード例 #8
0
import numpy as np
from scipy.integrate.quadpack import quad
import matplotlib.pyplot as plt
import matplotlib as mpl

mpl.style.use('default')

b = 5.0


# Function which is to be integrated
def func(x):
    return np.array(x**2)


x = np.linspace(-10, 10, 201)
integral, err = quad(func, 1, 5)
# Numerical quadrature that calculates the integral and error using fortran's quadpack library.

print("The integral is :", integral)
print("The error is :", err)

section = np.linspace(-5, 5, 20)

plt.plot(x, func(x), label='Function', color='b')
plt.fill_between(section, func(section), facecolor='red')
plt.grid()
plt.legend()
plt.title('Area under the curve')
plt.show()
コード例 #9
0
import scipy.integrate
from scipy.integrate.quadpack import quad

# print(help(quad))
scipy.special.b
[a, b] = [0, 1]


def f1(x): return np.exp(-x**2)


def kernel(x): return np.exp(-x**2)


quad(func, a, b, args=(), full_output=0, epsabs=1.49e-08, epsrel=1.49e-08,
     limit=50, points=None, weight=None, wvar=None, wopts=None, maxp1=50, limlst=50)


[xmin,xmax,nx]=[-2,2,100]

xx = np.linspace(xmin, xmax, nx)
yy = np.heaviside(xx, 0)
yint=np.trapz(yy,xx)
yy/=yint
ylist=[yy.tolist()]
ylist
xlist=[xx.tolist()]

# x2 = np.convolve(xx[::],xx[::])
nbox=4
for i in range(nbox):
コード例 #10
0
    Q_not.append(Q[len(Q) - i - 1][0])
    z_not.append(zs[len(zs) - i - 1])

# now interpolate
Q = interp1d(array(z_not), array(Q_not), kind='linear')  # p is rho UV(z)

TOTAL = plot(z_not, Q(z_not), 'g-', linewidth=3, zorder=-1)
#TOTAL = plot(zs,Q,'r--',linewidth=3,zorder=-1)

# SOLVE TAU:
z_integrate = arange(0.6, 14., 0.3)

os.system('rm tau_predict.txt')
for i in z_integrate:
    #print Q(i)
    A = quadpack.quad(tau, 0.1, i)[0]
    print >> open('tau_predict.txt', 'a'), i, A
"""
# LEGEND
I1=legend((TOTAL[0],LAEs[0],LBGS[0]),(r'Total LBGs',r'LAEs',r'Non-LAEs'), shadow = True, loc=3,numpoints=1)
ltext = gca().get_legend().get_texts()
setp(ltext[0], fontsize = 24, color='0.1')
setp(ltext[1], fontsize = 24, color='k')
#setp(ltext[2], fontsize = 15, color = '0.1')

frame=I1.get_frame()
frame.set_linewidth(0)
frame.set_visible(False)
"""

# Define the axis
コード例 #11
0
ファイル: stuff.py プロジェクト: mathiasose/TDT4195
def continuous_convolution(f, g):
    return lambda t: quad(lambda v: (f(v) * g(t - v)), -Inf, Inf)[0]
コード例 #12
0
def spectra(l, T):
    h = D['Planck constant'][0]
    k = D['Boltzmann constant'][0]
    c = D['speed of light in vacuum'][0]
    func = ((2.0 * np.pi * h * c**2) / (l**5)) / (np.exp(h * c /
                                                         (l * k * T)) - 1.0)
    return func


T1 = 5770.0  # Surface temperature of the sun!
T2 = 6000.0
T3 = 5000.0
T4 = 4000.0

int1, err1 = quad(spectra, 0.38e-6, 0.78e-6, args=(T1))

E1 = s * T1**4

l = np.linspace(0, 5e-6, 2**12 + 1)

s1 = spectra(l, T1)
s2 = spectra(l, T2)
s3 = spectra(l, T3)
s4 = spectra(l, T4)

print "The total Energy flux is :", E1
print "The Energy flux in the band is :", int1
print "The Energy flux Fraction is:", int1 / E1
print "The error is :", err1
コード例 #13
0
def arclength(df_x, df_y, t):
    integrand = lambda tau: np.sqrt( df_x(tau)**2+df_y(tau)**2 )
    return quad(integrand,0,t)[0]
コード例 #14
0
s = D['Stefan-Boltzmann constant'][0]

def spectra(l,T):
    h = D['Planck constant'][0]
    k = D['Boltzmann constant'][0]
    c = D['speed of light in vacuum'][0]
    func = ((2.0*np.pi*h*c**2)/(l**5))/(np.exp(h*c/(l*k*T)) - 1.0)
    return func

T1 = 5770.0 # Surface temperature of the sun!
T2 = 6000.0
T3 = 5000.0
T4 = 4000.0

int1,err1 = quad(spectra, 0.38e-6, 0.78e-6, args=(T1))

E1 = s*T1**4

l = np.linspace(0, 5e-6, 2**12 + 1)

s1 = spectra(l, T1)
s2 = spectra(l, T2)
s3 = spectra(l, T3)
s4 = spectra(l, T4)

print "The total Energy flux is :", E1
print "The Energy flux in the band is :", int1
print "The Energy flux Fraction is:", int1/E1
print "The error is :", err1
コード例 #15
0
ファイル: fdfunc.py プロジェクト: jzrake/micro-ph
def dfermi(n, eta, beta, use_timmes=False):
    res = quadpack.quad(fdfunc1, 0.0, quadpack.Inf, args=(n,eta,beta,False))
    return res[0]
コード例 #16
0
def outerIntegral(x, i, lagrange):
    ans, abserr = quad(innerIntegral, a=0, b=1 - x, args=(x, i, lagrange))
    return ans
コード例 #17
0
ファイル: Trazezoidal.py プロジェクト: VeerRavi97/Academy-BIT
# -*- coding: utf-8 -*-
"""
Created on Thu Aug  2 09:48:40 2018

@author: VP LAB
"""

import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
import numpy as np
from scipy.integrate.quadpack import quad


def func(x):
    return np.array(2.0 * x**2 - x - 4.0)


x = np.linspace(0, 10, 100)
integral = quad(func, 1, 5)

section = np.linspace(1, 5, 20)

plt.plot(x, func(x))
plt.fill_between(section, func(section), facecolor='g')
コード例 #18
0
def arclength(df_x, df_y, t):
    integrand = lambda tau: np.sqrt(df_x(tau)**2 + df_y(tau)**2)
    return quad(integrand, 0, t)[0]