NumericalCalculation

CNum

structure

This package includes four sub modules: function module, data type module, error feedback module and general module.

graph LR
CNum(CNum)--->Gn>General Module]
CNum(CNum)--->FC>Function Module]
CNum(CNum)--->DT>Data Type Module]
CNum(CNum)--->EF>Error Feedback Module]
style CNum fill:#407AFF,stroke:#333,stroke-width:1.5px,fill-opacity:1
style Gn fill:#f9f,stroke:#333,stroke-width:1px,fill-opacity:0.7
style FC fill:#f9f,stroke:#333,stroke-width:1px,fill-opacity:1
style DT fill:#f9f,stroke:#333,stroke-width:1px,fill-opacity:0.5
style EF fill:#f9f,stroke:#333,stroke-width:1px,fill-opacity:0.3

summary

Next, we will briefly introduce the functions of each module

function module

data type module

This module contains only one class called Matrix. In a future release, it will implement data transfer between each module. However, we do not currently apply this data type.

error feedback module

The error feedback mechanism is implemented through this module

general module

Usage and function

Elimination

This class contains several elimination methods for solving linear equations.

import CNum.Elimination as et
import CNum


augMatrix = \
    [[2, 1, 0, 0, -0.5],
     [0.5, 2, 0.5, 0, 0],
     [0, 0.5, 2, 0.5, 0],
     [0, 0, 1, 2, 0]]
et = CNum.Elimination(augMatrix)
print(et.gauss())
print(et.columnEliminate())
print(et.completeEliminate())

FuncAppro

This class contains a methods in order to construct an n-order polynomial.

import CNum.FuncAppro as fa
import CNum


xList = [-1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1]
yList = [-0.2209, 0.3295, 0.8826, 1.4392, 2.0003, 2.5645,
         3.1334, 3.7601, 4.2836]
pf = CNum.FuncAppro(xList, yList)
print(pf.PolyFitting(2))

Integral

This class contains several quadrature methods in order to calculate the approximation of the integrand.
But The integrand function needs to be provided by yourself.

import CNum.Integral as ig
import CNum
import math


def f(x):
    return (math.log(1+x))/(1+x)**2


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


itg = CNum.Integral([0, 1])
print(itg.Trapezoid(f, 4))
print(itg.TrapezoidHalf(f, 0.000001, 100))
print(itg.Simpson(f, 2))
print(itg.SimpsonHalf(f, 0.000001, 100))
print(itg.Romberg(f1, 0.0001, 100))

Interpolation

This class contains several interpolation methods in order to construct the interpolation polynomial function and calculate the value at X-point.

import CNum.Interpolation as ip
import CNum


xList = [0, 1, 2, 3]
yList = [0, 2, 3, 16]
itp = CNum.Interpolation(xList, yList)
print(itp.Lagrange(2.5))
print(itp.Newton(1.5))
xList = [0, 1]
yList = [0, 1]
yD = [3, 9]
print(itp.Hermite(0.5, yD))
print(itp.CubicSpline(2, 0, [1, 0]))

Iteration

This class contains several iterative methods in order to solve linear equations.

import CNum.Iteration as it
import CNum


augMat = \
    [[2, 1, 0, 0, -0.5],
     [0.5, 2, 0.5, 0, 0],
     [0, 0.5, 2, 0.5, 0],
     [0, 0, 1, 2, 0]]
xList = [1, 1, 1, 1]
it = CNum.Iteration(augMat, xList, 100, 0.000001)
print(it.Jacobi())
print(it.GaussSeidel())
print(it.SOR(1.2))

Power

This class contains several power methods in order to solve the maximum or minimum eigenvalue according to the mold and the corresponding eigenvector.

import CNum.Power as pr
import CNum


Matrix = \
    [[2, -1, 0],
    [0, 2, -1],
    [0, -1, 2]]
xList = [0, 0, 1]
np = CNum.Power()
np.setMatrix(Matrix)
np.setInitEigenvectors(xList)
print(np.NorPower())
print(np.OriginShift(2.9))
print(np.Aitken())
print(np.InversePower(2.93))

Root

This class contains several methods in order to solve the root of the nonlinear equation.

import CNum.Root as rt

No parameters are required!

import CNum


def f(x):
    return x**3+10*x-20


def f1(x):
    return 20/(x**2+10)


def fd(x):
    return 3*x**2 + 10


rt = CNum.Root()
print(rt.Bisection(f, [1, 2]))
print(rt.Steffensen(f1, 1.5))
print(Newton(f, fd, 1.5))
print(sct.Secant(f, [1.5, 2]))
print(Muller(f, [1.5, 1.75, 2]))

SquareRoot

This class contains several square root methods for solving linear equations that it contains a coefficient matrix with positive definite symmetry.

import CNum.SquareRoot as sr
import CNum


augMat = \
    [[2, 1, 0, 0, -0.5],
    [0.5, 2, 0.5, 0, 0],
    [0, 0.5, 2, 0.5, 0],
    [0, 0, 1, 2, 0]]
sqr = CNum.SquareRoot(augMat)
print(sqr.Cholesky())
print(sqr.LDLT())

TriDecomposition

This class contains several triangular decomposition methods for solving linear equations.

import CNum.TriDecomposition as td
import CNum


augMat = \
    [[2, 1, 0, 0, -0.5],
     [0.5, 2, 0.5, 0, 0],
     [0, 0.5, 2, 0.5, 0],
     [0, 0, 1, 2, 0]]

td = CNum.TriDecomposition(augMat)
print(td.Doolittle())
print(td.chase())