v1.0
v1.0 v1.0 v1.0 v1.0 v1.0 v1.0 v1.0 v1.0 v1.0 v1.0 v1.0 v1.0
This commit is contained in:
191
Otter/Otter.py
191
Otter/Otter.py
@@ -26,11 +26,10 @@ class Algebra:
|
|||||||
def __init__(self, function):
|
def __init__(self, function):
|
||||||
self.f = function
|
self.f = function
|
||||||
|
|
||||||
def riemann(self,interval):
|
def riemann(self,a,b,n=None):
|
||||||
|
|
||||||
a = interval[0]
|
if n is None:
|
||||||
b = interval[1]
|
n = 10**6
|
||||||
n = interval[2]
|
|
||||||
|
|
||||||
delta = (b-a)/n
|
delta = (b-a)/n
|
||||||
|
|
||||||
@@ -46,15 +45,14 @@ class Algebra:
|
|||||||
|
|
||||||
return integral
|
return integral
|
||||||
|
|
||||||
def simpson(self, interval):
|
def simpson(self,a,b,n=None):
|
||||||
|
|
||||||
|
if n is None:
|
||||||
|
n = 10**6
|
||||||
|
|
||||||
def x(i):
|
def x(i):
|
||||||
return a + i*h
|
return a + i*h
|
||||||
|
|
||||||
a = interval[0]
|
|
||||||
b = interval[1]
|
|
||||||
n = interval[2]
|
|
||||||
|
|
||||||
h = (b-a)/n
|
h = (b-a)/n
|
||||||
|
|
||||||
eta = 0
|
eta = 0
|
||||||
@@ -81,15 +79,13 @@ class Algebra:
|
|||||||
def __init__(self,function):
|
def __init__(self,function):
|
||||||
self.f = function
|
self.f = function
|
||||||
|
|
||||||
def riemann(self,x_interval,y_interval):
|
def riemann(self,a,b,c,d,n=None,m=None):
|
||||||
|
|
||||||
|
if n is None:
|
||||||
|
n = 10**4
|
||||||
|
|
||||||
a = x_interval[0]
|
if m is None:
|
||||||
b = x_interval[1]
|
m = n
|
||||||
n = x_interval[2]
|
|
||||||
|
|
||||||
c = y_interval[0]
|
|
||||||
d = y_interval[1]
|
|
||||||
m = y_interval[2]
|
|
||||||
|
|
||||||
dx = (b-a)/n
|
dx = (b-a)/n
|
||||||
dy = (d-c)/m
|
dy = (d-c)/m
|
||||||
@@ -109,15 +105,13 @@ class Algebra:
|
|||||||
|
|
||||||
return theta*(dx)*(dy)
|
return theta*(dx)*(dy)
|
||||||
|
|
||||||
def simpson(self,x_interval,y_interval):
|
def simpson(self,a,b,c,d,n=None,m=None):
|
||||||
|
|
||||||
|
if n is None:
|
||||||
|
n = 10**4
|
||||||
|
|
||||||
a = x_interval[0]
|
if m is None:
|
||||||
b = x_interval[1]
|
m = n
|
||||||
n = x_interval[2]
|
|
||||||
|
|
||||||
c = y_interval[0]
|
|
||||||
d = y_interval[1]
|
|
||||||
m = y_interval[2]
|
|
||||||
|
|
||||||
dx = (b-a)/n
|
dx = (b-a)/n
|
||||||
dy = (d-c)/m
|
dy = (d-c)/m
|
||||||
@@ -178,12 +172,10 @@ class Algebra:
|
|||||||
if function is not None:
|
if function is not None:
|
||||||
self.f = function
|
self.f = function
|
||||||
|
|
||||||
def bissec(self, interval):
|
def bissec(self,a,b,e=None):
|
||||||
""" invertal = [a,b,e] ; with 'a' being the first value of the interval, 'b' the last value of the interval and 'e' the precision of the procedure. """
|
|
||||||
|
if e is None:
|
||||||
a = interval[0]
|
e = 10**(-6)
|
||||||
b = interval[1]
|
|
||||||
e = interval[2]
|
|
||||||
|
|
||||||
fa = self.f(a)
|
fa = self.f(a)
|
||||||
|
|
||||||
@@ -208,10 +200,10 @@ class Algebra:
|
|||||||
def d(self, x, e):
|
def d(self, x, e):
|
||||||
return (self.f(x + e) - self.f(x - e))/(2*e)
|
return (self.f(x + e) - self.f(x - e))/(2*e)
|
||||||
|
|
||||||
def newton(self, interval):
|
def newton(self,a,e=None):
|
||||||
|
|
||||||
a = interval[0]
|
if e is None:
|
||||||
e = interval[1]
|
e = 10**(-6)
|
||||||
|
|
||||||
fa = self.f(a)
|
fa = self.f(a)
|
||||||
da = self.d(a,e)
|
da = self.d(a,e)
|
||||||
@@ -227,11 +219,10 @@ class Algebra:
|
|||||||
|
|
||||||
return a
|
return a
|
||||||
|
|
||||||
def bissec_newton(self, interval):
|
def bissec_newton(self,a,b,e=None):
|
||||||
|
|
||||||
a = interval[0]
|
if e is None:
|
||||||
b = interval[1]
|
e = 10**(-6)
|
||||||
e = interval[2]
|
|
||||||
|
|
||||||
fa = self.f(a)
|
fa = self.f(a)
|
||||||
|
|
||||||
@@ -271,12 +262,10 @@ class Algebra:
|
|||||||
def __init__(self, function):
|
def __init__(self, function):
|
||||||
self.f = function
|
self.f = function
|
||||||
|
|
||||||
def euler(self, interval):
|
def euler(self,a,y,b,n=None):
|
||||||
|
|
||||||
a = interval[0]
|
if n is None:
|
||||||
b = interval[1]
|
n = 10**7
|
||||||
y = interval[2]
|
|
||||||
n = int(interval[3])
|
|
||||||
|
|
||||||
dx = (b-a)/n
|
dx = (b-a)/n
|
||||||
|
|
||||||
@@ -289,12 +278,10 @@ class Algebra:
|
|||||||
|
|
||||||
return y
|
return y
|
||||||
|
|
||||||
def runge(self, interval):
|
def runge(self,a,y,b,n=None):
|
||||||
|
|
||||||
a = interval[0]
|
if n is None:
|
||||||
b = interval[1]
|
n = 10**7
|
||||||
y = interval[2]
|
|
||||||
n = int(interval[3])
|
|
||||||
|
|
||||||
dx = (b-a)/n
|
dx = (b-a)/n
|
||||||
|
|
||||||
@@ -315,6 +302,59 @@ class Interpolation:
|
|||||||
self.data = data
|
self.data = data
|
||||||
self.polinomial = self.Polinomial(self.data)
|
self.polinomial = self.Polinomial(self.data)
|
||||||
|
|
||||||
|
def minimus(self,x):
|
||||||
|
|
||||||
|
theta = 0
|
||||||
|
# somatorio de x
|
||||||
|
for i in range(self.data.shape[0]):
|
||||||
|
|
||||||
|
theta += self.data[i][0]
|
||||||
|
|
||||||
|
eta = 0
|
||||||
|
#somatorio de y
|
||||||
|
for i in range(self.data.shape[0]):
|
||||||
|
|
||||||
|
eta += self.data[i][1]
|
||||||
|
|
||||||
|
sigma = 0
|
||||||
|
#somatorio de xy
|
||||||
|
for i in range(self.data.shape[0]):
|
||||||
|
|
||||||
|
sigma += self.data[i][0]*self.data[i][1]
|
||||||
|
|
||||||
|
omega = 0
|
||||||
|
#somatorio de x^2
|
||||||
|
for i in range(self.data.shape[0]):
|
||||||
|
|
||||||
|
omega += self.data[i][0]**2
|
||||||
|
|
||||||
|
|
||||||
|
self.a = (self.data.shape[0]*sigma - theta*eta)/(self.data.shape[0]*omega - (theta**2))
|
||||||
|
|
||||||
|
self.b = (theta*sigma - eta*omega)/((theta**2) - self.data.shape[0]*omega)
|
||||||
|
|
||||||
|
ym = 0
|
||||||
|
|
||||||
|
for i in range(self.data.shape[0]):
|
||||||
|
|
||||||
|
ym += self.data[i][1]/self.data.shape[0]
|
||||||
|
|
||||||
|
sqreq = 0
|
||||||
|
|
||||||
|
for i in range(self.data.shape[0]):
|
||||||
|
|
||||||
|
sqreq += ((self.a*self.data[i][0] + self.b) - ym)**2
|
||||||
|
|
||||||
|
sqtot = 0
|
||||||
|
|
||||||
|
for i in range(self.data.shape[0]):
|
||||||
|
|
||||||
|
sqtot += (self.data[i][1] - ym)**2
|
||||||
|
|
||||||
|
self.r2 = sqreq/sqtot
|
||||||
|
|
||||||
|
return self.a*x + self.b
|
||||||
|
|
||||||
class Polinomial:
|
class Polinomial:
|
||||||
|
|
||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
@@ -437,57 +477,4 @@ class Interpolation:
|
|||||||
y += d[i+1][0]*mult
|
y += d[i+1][0]*mult
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
return y
|
return y
|
||||||
|
|
||||||
def minimus(self,x):
|
|
||||||
|
|
||||||
theta = 0
|
|
||||||
# somatorio de x
|
|
||||||
for i in range(self.data.shape[0]):
|
|
||||||
|
|
||||||
theta += self.data[i][0]
|
|
||||||
|
|
||||||
eta = 0
|
|
||||||
#somatorio de y
|
|
||||||
for i in range(self.data.shape[0]):
|
|
||||||
|
|
||||||
eta += self.data[i][1]
|
|
||||||
|
|
||||||
sigma = 0
|
|
||||||
#somatorio de xy
|
|
||||||
for i in range(self.data.shape[0]):
|
|
||||||
|
|
||||||
sigma += self.data[i][0]*self.data[i][1]
|
|
||||||
|
|
||||||
omega = 0
|
|
||||||
#somatorio de x^2
|
|
||||||
for i in range(self.data.shape[0]):
|
|
||||||
|
|
||||||
omega += self.data[i][0]**2
|
|
||||||
|
|
||||||
|
|
||||||
self.a = (self.data.shape[0]*sigma - theta*eta)/(self.data.shape[0]*omega - (theta**2))
|
|
||||||
|
|
||||||
self.b = (theta*sigma - eta*omega)/((theta**2) - self.data.shape[0]*omega)
|
|
||||||
|
|
||||||
ym = 0
|
|
||||||
|
|
||||||
for i in range(self.data.shape[0]):
|
|
||||||
|
|
||||||
ym += self.data[i][1]/self.data.shape[0]
|
|
||||||
|
|
||||||
sqreq = 0
|
|
||||||
|
|
||||||
for i in range(self.data.shape[0]):
|
|
||||||
|
|
||||||
sqreq += ((self.a*self.data[i][0] + self.b) - ym)**2
|
|
||||||
|
|
||||||
sqtot = 0
|
|
||||||
|
|
||||||
for i in range(self.data.shape[0]):
|
|
||||||
|
|
||||||
sqtot += (self.data[i][1] - ym)**2
|
|
||||||
|
|
||||||
self.r2 = sqreq/sqtot
|
|
||||||
|
|
||||||
return self.a*x + self.b
|
|
||||||
@@ -1,2 +1,2 @@
|
|||||||
from .Otter import Algebra
|
from .Otter import Algebra as algebra
|
||||||
from .Otter import Interpolation
|
from .Otter import Interpolation as interpolation
|
||||||
78
README.md
78
README.md
@@ -1,50 +1,68 @@
|
|||||||
# Numeric Calculus
|
# Otter - Numeric Calculus
|
||||||
|
|
||||||
This is a Repository of Python Packages for Numeric Calculus. It contains two packages: Seals and Otter.
|
This python package is made for applied Numeric Calculus of Algebra Functions. It is made with the following objectives in mind:
|
||||||
|
|
||||||
## Seals
|
* Receive one variable function from user input
|
||||||
|
|
||||||
|
* Receive two variable function from user input
|
||||||
|
|
||||||
The package Seals is made for Linear Algebra. It's able to:
|
* Performe derivatives with one variable functions
|
||||||
|
|
||||||
* Scan *csv* files to make a numpy matrix.
|
* Performe integral with received functions
|
||||||
|
|
||||||
* Write a matrix into a *csv* file
|
|
||||||
|
|
||||||
* Insert user input into a matrix or a vector.
|
|
||||||
|
|
||||||
* Use methods to proccess the matrices.
|
* Use methods to proccess the matrices.
|
||||||
* Identity Matrix
|
|
||||||
* Gauss Elimination
|
|
||||||
* Inverse Matrix
|
|
||||||
* Cholesky Decomposition
|
|
||||||
* LU Decomposition
|
|
||||||
* Cramer
|
|
||||||
|
|
||||||
### Syntax
|
* Find root of functions throw method of bissection and method of newton
|
||||||
|
|
||||||
The function *scan* has the following syntax `scan(path)`, where `path` is the path to your directory.
|
* Solve Diferential Equations throw method of euler and runge
|
||||||
|
|
||||||
The function *solution* has the following syntax `write(array,path)`, where `array` is the matrix that you desire to output and `path` is the path to your directory.
|
* Performe Minimus Interpolation and Polinomial Interpolation
|
||||||
|
|
||||||
The python class *Insert* has a method for *matrix* and another for *vector*, and it has the following syntax `Insert.method(array)`, where `Insert` is the *Python Class* and `method` is either a `matrix` or a `vector` and `array` is either a *matrix* or a *vector*.
|
## Syntax
|
||||||
|
|
||||||
### Processes
|
To initialize a Otter instance linked to functions use the following syntax `otr = Otter.algebra(f)`, where `otr` will be a arbitrary name for the instance and `f` is a function of *one variable*.
|
||||||
|
|
||||||
The python class *process* has all the methods described in the first session.
|
To initialize a Otter instance linked to data and interpolation use the following syntax `otr = Otter.interpolation(data)`, where `otr` will be a arbitrary name for the instance and data will be a *numpy* matrix where the first columns has to contain the values for `x` and the second column contains the values for `y`.
|
||||||
|
|
||||||
To call the method use a syntax like `sl = Seals.process()`, where `sl` is an instance and to use a method you have to append the method in front of the instance like: `sl.identity(array)`.
|
### Algebra
|
||||||
|
|
||||||
* The method *identity* returns a *numpy* identity matrix of the order of the matrix passed into to it, and it has the following syntax `sl.identity(array)`, which `array` is a square matrix.
|
Algebra is a Python Class where some of the features described previously are defined as Classes as well, like: `Integral`, `Roots`, `EDO` (diferential equations).
|
||||||
|
|
||||||
* The method *gauss* returns a *numpy* vector containing the vector of variables from the augmented matrix. `sl.gauss(matrix)`, which `matrix` is the augmented matrix.
|
#### Integral
|
||||||
|
|
||||||
* The method *inverse* returns a *numpy* inverse matrix of the matrix passed into to it, and it has the following syntax `sl.inverse(matrix)`, which `matrix` is a square matrix.
|
To call the class *Integral* append the sufix with lower case in front of the instance like: `otr.integral`. The Integral class has two other class defined inside, `Simple` and `Double`, to call them append the sufix with lower case in front as `otr.integral.simple` or `otr.integral.double`. Then pick between Riemann's Method or Simpson's Method by appending the sufix `riemann` or `simpson` as well.
|
||||||
|
|
||||||
* The method *cholesky* returns a *numpy* vector containing the vector of variables from the coefficient matrix and the constants vector, and it has the following syntax `sl.cholesky(A,b)`, which `A` is the coefficient matrix and `b` is the constants vector.
|
After that the syntax will be something like `otr.integral.double.riemann(a,b,c,d,n,m)`, where `a` and `c` will be the first value of the interval of integration respectively in x and y, `b` and `d` will be the last, `n` and `m` will be the number of partitions.
|
||||||
|
|
||||||
* The method *decomposition* returns a *numpy* vector containing the vector of variables from the coefficient matrix and the constants vector, and it has the following syntax `sl.cholesky(A,b)`, which `A` is the coefficient matrix and `b` is the constants vector.
|
|
||||||
|
|
||||||
* The method *cramer* returns a *numpy* vector containing the vector of variables from the coefficient matrix and the constants vector, and it has the following syntax `sl.cholesky(A,b)`, which `A` is the coefficient matrix and `b` is the constants vector.
|
The syntax for one variable integrations will be `otr.integral.simple.riemann(a,b,n)`.
|
||||||
|
|
||||||
|
If `n` is not defined the standart value in 10^6 partitions for one variable and 10^4 for double. And if `m` is not defined the standart value will be equal to `n`.
|
||||||
|
|
||||||
|
#### Roots
|
||||||
|
|
||||||
|
To call the class *Root* append the sufix with lower case in front of the instance like: `otr.roots`. The Roots class has three methods defined inside, `bissec`, `newton` and `bissec_newton`, to call them append the sufix with lower case in front as `otr.roots.bissec` or `otr.roots.newton` or even `otr.roots.bissecnewton`.
|
||||||
|
|
||||||
|
The syntax for the bissection method and bissec_newton is equal to `otr.roots.bissec(a,b,e)` and `otr.roots.bissec_newton(a,b,e)`, where `a` is the first element of the interval containing the root and `b` is the last, `e` being the precision.
|
||||||
|
|
||||||
|
The syntax for the newton method is equal to `otr.roots.newton(a,e)`, where `a` is the element closest to the root and `e` is the precision.
|
||||||
|
|
||||||
|
If `e` is not defined the standart value is 10^(-6).
|
||||||
|
|
||||||
|
#### Diferential Equations
|
||||||
|
|
||||||
|
To call the class *EDO* (*E*quações *D*iferenciais *O*rdinárias) append the sufix with lower case in front of the instance like: `otr.edo`. The *EDO* class has two methods defined inside: `euler` and `runge`, to call them append the sufix with lower case in front as `otr.edo.euler` or `otr.edo.runge`.
|
||||||
|
|
||||||
|
The syntax for the diferential equations method is equal to `otr.edo.euler(a,y,b,n)` or `otr.edo.runge(a,y,b,n)`, where `a` and `y` will be the inintial point and `b` is the value in *x* which you want to know the corresponding value in *y* and `n` is the number of operations.
|
||||||
|
|
||||||
|
If `n` is not defined the standart value is 10^7.
|
||||||
|
|
||||||
|
### Interpolation
|
||||||
|
|
||||||
|
The python class *Interpolation* is divided in one method, minimus interpolation, and one class, polinomial interpolation.
|
||||||
|
|
||||||
|
To call the method *minimus* use a syntax like `otr = Otter.interpolation(data)`, where `otr` is an instance and append the method in front of the instance like: `otr.minimus(x)`, where *x* is value of *f(x)* you want to estimate.
|
||||||
|
|
||||||
|
To call the class *Polinomial* append the sufix with lower case in front of the instance like: `otr.polinomial`. The *Polinomial* class has four methods defined inside: `vandermonde`, `lagrange`, `newton` and `gregory`, to call them append the sufix with lower case in front like `otr.edo.gregory(x)` where *x* is value of *f(x)* you want to estimate.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@@ -54,4 +72,4 @@ To install the package from source `cd` into the directory and run:
|
|||||||
|
|
||||||
or run
|
or run
|
||||||
|
|
||||||
`pip install Numeric-Calculus_HideyoshiNakazone`
|
`pip install yoshi-otter`
|
||||||
|
|||||||
480
build/lib/Otter/Otter.py
Normal file
480
build/lib/Otter/Otter.py
Normal file
@@ -0,0 +1,480 @@
|
|||||||
|
import math
|
||||||
|
import numpy as np
|
||||||
|
import Seals
|
||||||
|
|
||||||
|
sl = Seals.method()
|
||||||
|
|
||||||
|
class Algebra:
|
||||||
|
|
||||||
|
def __init__(self, function):
|
||||||
|
self.f = function
|
||||||
|
self.integral = self.Integral(self.f)
|
||||||
|
self.roots = self.Roots(self.f)
|
||||||
|
self.edo = self.Edo(self.f)
|
||||||
|
|
||||||
|
def d(self, x, e):
|
||||||
|
return (self.f(x + e) - self.f(x - e))/(2*e)
|
||||||
|
|
||||||
|
class Integral:
|
||||||
|
|
||||||
|
def __init__(self,function):
|
||||||
|
self.f = function
|
||||||
|
self.simple = self.Simple(function)
|
||||||
|
self.double = self.Double(function)
|
||||||
|
|
||||||
|
class Simple:
|
||||||
|
def __init__(self, function):
|
||||||
|
self.f = function
|
||||||
|
|
||||||
|
def riemann(self,a,b,n=None):
|
||||||
|
|
||||||
|
if n is None:
|
||||||
|
n = 10**6
|
||||||
|
|
||||||
|
delta = (b-a)/n
|
||||||
|
|
||||||
|
psi = a
|
||||||
|
theta = 0
|
||||||
|
|
||||||
|
while((psi+delta) <= b):
|
||||||
|
|
||||||
|
theta += (self.f(psi) + self.f(psi + delta))/2
|
||||||
|
psi += delta
|
||||||
|
|
||||||
|
integral = delta*theta
|
||||||
|
|
||||||
|
return integral
|
||||||
|
|
||||||
|
def simpson(self,a,b,n=None):
|
||||||
|
|
||||||
|
if n is None:
|
||||||
|
n = 10**6
|
||||||
|
|
||||||
|
def x(i):
|
||||||
|
return a + i*h
|
||||||
|
|
||||||
|
h = (b-a)/n
|
||||||
|
|
||||||
|
eta = 0
|
||||||
|
theta = 0
|
||||||
|
|
||||||
|
psi = 1
|
||||||
|
kappa = 1
|
||||||
|
|
||||||
|
while(psi <= (n/2)):
|
||||||
|
|
||||||
|
eta = eta + self.f(x(2*psi - 1))
|
||||||
|
psi = psi + 1
|
||||||
|
|
||||||
|
while(kappa <= ((n/2)-1)):
|
||||||
|
|
||||||
|
theta = theta + self.f(x(2*kappa))
|
||||||
|
kappa = kappa + 1
|
||||||
|
|
||||||
|
return (h/3)*( self.f(x(0)) + self.f(x(n)) + 4*eta + 2*theta)
|
||||||
|
|
||||||
|
|
||||||
|
class Double:
|
||||||
|
|
||||||
|
def __init__(self,function):
|
||||||
|
self.f = function
|
||||||
|
|
||||||
|
def riemann(self,a,b,c,d,n=None,m=None):
|
||||||
|
|
||||||
|
if n is None:
|
||||||
|
n = 10**4
|
||||||
|
|
||||||
|
if m is None:
|
||||||
|
m = n
|
||||||
|
|
||||||
|
dx = (b-a)/n
|
||||||
|
dy = (d-c)/m
|
||||||
|
kappa = a
|
||||||
|
psi = c
|
||||||
|
theta = 0
|
||||||
|
|
||||||
|
while((psi + dy) < d):
|
||||||
|
|
||||||
|
while((kappa + dx) < b):
|
||||||
|
|
||||||
|
theta = theta + self.f(kappa, psi)
|
||||||
|
kappa = kappa + dx
|
||||||
|
|
||||||
|
psi = psi + dy
|
||||||
|
kappa = a
|
||||||
|
|
||||||
|
return theta*(dx)*(dy)
|
||||||
|
|
||||||
|
def simpson(self,a,b,c,d,n=None,m=None):
|
||||||
|
|
||||||
|
if n is None:
|
||||||
|
n = 10**4
|
||||||
|
|
||||||
|
if m is None:
|
||||||
|
m = n
|
||||||
|
|
||||||
|
dx = (b-a)/n
|
||||||
|
dy = (d-c)/m
|
||||||
|
|
||||||
|
def x(i):
|
||||||
|
|
||||||
|
x = a + i*dx
|
||||||
|
|
||||||
|
return x
|
||||||
|
|
||||||
|
def y(i):
|
||||||
|
|
||||||
|
y = c + i*dy
|
||||||
|
|
||||||
|
return y
|
||||||
|
|
||||||
|
def g(i):
|
||||||
|
|
||||||
|
sigma = 0
|
||||||
|
upsilon = 0
|
||||||
|
|
||||||
|
zeta = 1
|
||||||
|
csi = 1
|
||||||
|
|
||||||
|
while(zeta <= (m/2)):
|
||||||
|
|
||||||
|
sigma += self.f(x(i),y(2*zeta - 1))
|
||||||
|
zeta += 1
|
||||||
|
|
||||||
|
while(csi <= ((m/2)-1)):
|
||||||
|
|
||||||
|
upsilon += self.f(x(i),y(2*csi))
|
||||||
|
csi += 1
|
||||||
|
|
||||||
|
return (dy/3)*( self.f(x(i),y(0)) + self.f(x(i),y(m)) + 4*sigma + 2*upsilon )
|
||||||
|
|
||||||
|
eta = 0
|
||||||
|
theta = 0
|
||||||
|
|
||||||
|
psi = 1
|
||||||
|
kappa = 1
|
||||||
|
|
||||||
|
while(psi <= (n/2)):
|
||||||
|
|
||||||
|
eta += g(2*psi - 1)
|
||||||
|
psi += 1
|
||||||
|
|
||||||
|
while(kappa <= ((n/2)-1)):
|
||||||
|
|
||||||
|
theta += g(2*kappa)
|
||||||
|
kappa += 1
|
||||||
|
|
||||||
|
return (dx/3)*( g(0) + g(n) + 4*eta + 2*theta)
|
||||||
|
|
||||||
|
class Roots:
|
||||||
|
|
||||||
|
def __init__(self, function=None):
|
||||||
|
if function is not None:
|
||||||
|
self.f = function
|
||||||
|
|
||||||
|
def bissec(self,a,b,e=None):
|
||||||
|
|
||||||
|
if e is None:
|
||||||
|
e = 10**(-6)
|
||||||
|
|
||||||
|
fa = self.f(a)
|
||||||
|
|
||||||
|
while abs(a-b) > e:
|
||||||
|
|
||||||
|
c = (a+b)/2
|
||||||
|
fc = self.f(c)
|
||||||
|
|
||||||
|
if (fa*fc) < 0:
|
||||||
|
|
||||||
|
b = c
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
|
a = c
|
||||||
|
fa = fc
|
||||||
|
|
||||||
|
c = (a+b)/2
|
||||||
|
|
||||||
|
return c
|
||||||
|
|
||||||
|
def d(self, x, e):
|
||||||
|
return (self.f(x + e) - self.f(x - e))/(2*e)
|
||||||
|
|
||||||
|
def newton(self,a,e=None):
|
||||||
|
|
||||||
|
if e is None:
|
||||||
|
e = 10**(-6)
|
||||||
|
|
||||||
|
fa = self.f(a)
|
||||||
|
da = self.d(a,e)
|
||||||
|
b = a - fa/da
|
||||||
|
|
||||||
|
|
||||||
|
while abs(a-b) > e:
|
||||||
|
|
||||||
|
b = a
|
||||||
|
a -= (fa/da)
|
||||||
|
fa = self.f(a)
|
||||||
|
da = self.d(a,e)
|
||||||
|
|
||||||
|
return a
|
||||||
|
|
||||||
|
def bissec_newton(self,a,b,e=None):
|
||||||
|
|
||||||
|
if e is None:
|
||||||
|
e = 10**(-6)
|
||||||
|
|
||||||
|
fa = self.f(a)
|
||||||
|
|
||||||
|
c = (a+b)/2 # 'c' é a raiz calculada
|
||||||
|
|
||||||
|
while abs(a-b) > 0.1:
|
||||||
|
|
||||||
|
fc = self.f(c)
|
||||||
|
|
||||||
|
if fa*fc < 0:
|
||||||
|
|
||||||
|
b = c
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
|
a = c
|
||||||
|
fa = self.f(a)
|
||||||
|
|
||||||
|
c = (a+b)/2
|
||||||
|
|
||||||
|
fc = self.f(c)
|
||||||
|
dc = self.d(c,e)
|
||||||
|
h = c - fc/dc # 'h' é uma variável de controle
|
||||||
|
|
||||||
|
while abs(c-h) > e:
|
||||||
|
|
||||||
|
h = c
|
||||||
|
c -= (fc/dc)
|
||||||
|
fc = self.f(c)
|
||||||
|
dc = self.d(c,e)
|
||||||
|
|
||||||
|
return (c)
|
||||||
|
|
||||||
|
|
||||||
|
class Edo:
|
||||||
|
|
||||||
|
def __init__(self, function):
|
||||||
|
self.f = function
|
||||||
|
|
||||||
|
def euler(self,a,y,b,n=None):
|
||||||
|
|
||||||
|
if n is None:
|
||||||
|
n = 10**7
|
||||||
|
|
||||||
|
dx = (b-a)/n
|
||||||
|
|
||||||
|
def x(i):
|
||||||
|
return a + i*dx
|
||||||
|
|
||||||
|
for i in range(n):
|
||||||
|
|
||||||
|
y = y + (self.f(x(i),y))*dx
|
||||||
|
|
||||||
|
return y
|
||||||
|
|
||||||
|
def runge(self,a,y,b,n=None):
|
||||||
|
|
||||||
|
if n is None:
|
||||||
|
n = 10**7
|
||||||
|
|
||||||
|
dx = (b-a)/n
|
||||||
|
|
||||||
|
def x(i):
|
||||||
|
return (a + i*dx)
|
||||||
|
|
||||||
|
for i in range(n):
|
||||||
|
|
||||||
|
y = y + (dx/2)*(self.f(x(i),y)+self.f(x(i+1),(y+(dx*self.f(x(i),y)))))
|
||||||
|
|
||||||
|
return y
|
||||||
|
|
||||||
|
class Interpolation:
|
||||||
|
""" Data should be organized in two columns: X and Y"""
|
||||||
|
|
||||||
|
def __init__(self, data):
|
||||||
|
|
||||||
|
self.data = data
|
||||||
|
self.polinomial = self.Polinomial(self.data)
|
||||||
|
|
||||||
|
def minimus(self,x):
|
||||||
|
|
||||||
|
theta = 0
|
||||||
|
# somatorio de x
|
||||||
|
for i in range(self.data.shape[0]):
|
||||||
|
|
||||||
|
theta += self.data[i][0]
|
||||||
|
|
||||||
|
eta = 0
|
||||||
|
#somatorio de y
|
||||||
|
for i in range(self.data.shape[0]):
|
||||||
|
|
||||||
|
eta += self.data[i][1]
|
||||||
|
|
||||||
|
sigma = 0
|
||||||
|
#somatorio de xy
|
||||||
|
for i in range(self.data.shape[0]):
|
||||||
|
|
||||||
|
sigma += self.data[i][0]*self.data[i][1]
|
||||||
|
|
||||||
|
omega = 0
|
||||||
|
#somatorio de x^2
|
||||||
|
for i in range(self.data.shape[0]):
|
||||||
|
|
||||||
|
omega += self.data[i][0]**2
|
||||||
|
|
||||||
|
|
||||||
|
self.a = (self.data.shape[0]*sigma - theta*eta)/(self.data.shape[0]*omega - (theta**2))
|
||||||
|
|
||||||
|
self.b = (theta*sigma - eta*omega)/((theta**2) - self.data.shape[0]*omega)
|
||||||
|
|
||||||
|
ym = 0
|
||||||
|
|
||||||
|
for i in range(self.data.shape[0]):
|
||||||
|
|
||||||
|
ym += self.data[i][1]/self.data.shape[0]
|
||||||
|
|
||||||
|
sqreq = 0
|
||||||
|
|
||||||
|
for i in range(self.data.shape[0]):
|
||||||
|
|
||||||
|
sqreq += ((self.a*self.data[i][0] + self.b) - ym)**2
|
||||||
|
|
||||||
|
sqtot = 0
|
||||||
|
|
||||||
|
for i in range(self.data.shape[0]):
|
||||||
|
|
||||||
|
sqtot += (self.data[i][1] - ym)**2
|
||||||
|
|
||||||
|
self.r2 = sqreq/sqtot
|
||||||
|
|
||||||
|
return self.a*x + self.b
|
||||||
|
|
||||||
|
class Polinomial:
|
||||||
|
|
||||||
|
def __init__(self, data):
|
||||||
|
self.data = data
|
||||||
|
|
||||||
|
def vandermonde(self, x):
|
||||||
|
|
||||||
|
matrix = np.zeros((self.data.shape[0],self.data.shape[0]))
|
||||||
|
|
||||||
|
for k in range(0, self.data.shape[0]):
|
||||||
|
|
||||||
|
matrix[:,k] = self.data[:,0]**k
|
||||||
|
|
||||||
|
self.A = sl.gauss(np.c_[matrix,self.data[:,1]])
|
||||||
|
|
||||||
|
y = 0
|
||||||
|
|
||||||
|
for i in range(0,self.A.shape[0]):
|
||||||
|
|
||||||
|
y += self.A[i]*(x**i)
|
||||||
|
|
||||||
|
return float(y)
|
||||||
|
|
||||||
|
def lagrange(self, x):
|
||||||
|
|
||||||
|
data_x = self.data[:,0]
|
||||||
|
data_y = self.data[:,1]
|
||||||
|
|
||||||
|
def L(k,x):
|
||||||
|
|
||||||
|
up = down = 1
|
||||||
|
|
||||||
|
for i in [x for x in range(data_x.shape[0]) if x != k]:
|
||||||
|
up = up*(x - data_x[i])
|
||||||
|
|
||||||
|
for i in [x for x in range(data_x.shape[0]) if x != k]:
|
||||||
|
down = down*(data_x[k] - data_x[i])
|
||||||
|
|
||||||
|
return up/down
|
||||||
|
|
||||||
|
y = 0
|
||||||
|
|
||||||
|
for i in range(data_x.shape[0]):
|
||||||
|
|
||||||
|
y += data_y[i]*L(i,x)
|
||||||
|
|
||||||
|
return y
|
||||||
|
|
||||||
|
def newton(self,x):
|
||||||
|
|
||||||
|
d = np.array(np.zeros((self.data.shape[0],self.data.shape[0])))
|
||||||
|
|
||||||
|
d[0] = self.data[:,1]
|
||||||
|
|
||||||
|
i = j = 0
|
||||||
|
|
||||||
|
while (i < self.data.shape[0]):
|
||||||
|
|
||||||
|
while (j < (self.data.shape[0]-(i+1))):
|
||||||
|
|
||||||
|
d[i+1][j] = (d[i][j+1] - d[i][j])/(self.data[(i+1)+j][0]-self.data[j][0])
|
||||||
|
j += 1
|
||||||
|
|
||||||
|
i += 1
|
||||||
|
j = 0
|
||||||
|
|
||||||
|
def f(x):
|
||||||
|
|
||||||
|
y = d[0][0]
|
||||||
|
i = 0
|
||||||
|
|
||||||
|
while ((i+1) < self.data.shape[0]):
|
||||||
|
|
||||||
|
mult = 1
|
||||||
|
k = 0
|
||||||
|
while (k <= i):
|
||||||
|
mult = mult*(x - self.data[k][0])
|
||||||
|
k += 1
|
||||||
|
|
||||||
|
y += d[i+1][0]*mult
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
return y
|
||||||
|
|
||||||
|
self.f = f
|
||||||
|
|
||||||
|
return f(x)
|
||||||
|
|
||||||
|
def gregory(self,x):
|
||||||
|
|
||||||
|
h = self.data[0][0] - self.data[1][0]
|
||||||
|
|
||||||
|
d = np.array(np.zeros((self.data.shape[0],self.data.shape[0])))
|
||||||
|
|
||||||
|
d[0] = self.data[:,1]
|
||||||
|
|
||||||
|
i = j = 0
|
||||||
|
|
||||||
|
while (i < self.data.shape[0]):
|
||||||
|
|
||||||
|
while (j < (self.data.shape[0]-(i+1))):
|
||||||
|
|
||||||
|
d[i+1][j] = (d[i][j+1] - d[i][j])/((i+1)*h)
|
||||||
|
j += 1
|
||||||
|
|
||||||
|
i += 1
|
||||||
|
j = 0
|
||||||
|
|
||||||
|
y = d[0][0]
|
||||||
|
i = 0
|
||||||
|
|
||||||
|
while ((i+1) < self.data.shape[0]):
|
||||||
|
|
||||||
|
mult = 1
|
||||||
|
k = 0
|
||||||
|
while (k <= i):
|
||||||
|
mult = mult*(x - self.data[k][0])
|
||||||
|
k += 1
|
||||||
|
|
||||||
|
y += d[i+1][0]*mult
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
return y
|
||||||
2
build/lib/Otter/__init__.py
Normal file
2
build/lib/Otter/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
from .Otter import Algebra as algebra
|
||||||
|
from .Otter import Interpolation as interpolation
|
||||||
BIN
dist/yoshi-otter-1.1.tar.gz
vendored
Normal file
BIN
dist/yoshi-otter-1.1.tar.gz
vendored
Normal file
Binary file not shown.
BIN
dist/yoshi_otter-1.1-py3-none-any.whl
vendored
Normal file
BIN
dist/yoshi_otter-1.1-py3-none-any.whl
vendored
Normal file
Binary file not shown.
7
setup.py
7
setup.py
@@ -4,11 +4,11 @@ with open("README.md", "r") as fh:
|
|||||||
long_description = fh.read()
|
long_description = fh.read()
|
||||||
|
|
||||||
setuptools.setup(
|
setuptools.setup(
|
||||||
name="Otter", # Replace with your own username
|
name="yoshi-otter", # Replace with your own username
|
||||||
version="1.0",
|
version="1.1",
|
||||||
author="Vitor Hideyoshi",
|
author="Vitor Hideyoshi",
|
||||||
author_email="vitor.h.n.batista@gmail.com",
|
author_email="vitor.h.n.batista@gmail.com",
|
||||||
description="Algebra Functions Python Module for Numeric Calculus",
|
description="Numeric Calculus python module in the topic of Algebra Functions",
|
||||||
long_description=long_description,
|
long_description=long_description,
|
||||||
long_description_content_type="text/markdown",
|
long_description_content_type="text/markdown",
|
||||||
url="https://github.com/HideyoshiNakazone/Otter-NumericCalculus.git",
|
url="https://github.com/HideyoshiNakazone/Otter-NumericCalculus.git",
|
||||||
@@ -23,5 +23,6 @@ setuptools.setup(
|
|||||||
install_requires=[
|
install_requires=[
|
||||||
'numpy',
|
'numpy',
|
||||||
'pandas',
|
'pandas',
|
||||||
|
'yoshi-seals'
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
91
yoshi_otter.egg-info/PKG-INFO
Normal file
91
yoshi_otter.egg-info/PKG-INFO
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
Metadata-Version: 2.1
|
||||||
|
Name: yoshi-otter
|
||||||
|
Version: 1.1
|
||||||
|
Summary: Numeric Calculus python module in the topic of Algebra Functions
|
||||||
|
Home-page: https://github.com/HideyoshiNakazone/Otter-NumericCalculus.git
|
||||||
|
Author: Vitor Hideyoshi
|
||||||
|
Author-email: vitor.h.n.batista@gmail.com
|
||||||
|
License: UNKNOWN
|
||||||
|
Description: # Otter - Numeric Calculus
|
||||||
|
|
||||||
|
This python package is made for applied Numeric Calculus of Algebra Functions. It is made with the following objectives in mind:
|
||||||
|
|
||||||
|
* Receive one variable function from user input
|
||||||
|
|
||||||
|
* Receive two variable function from user input
|
||||||
|
|
||||||
|
* Performe derivatives with one variable functions
|
||||||
|
|
||||||
|
* Performe integral with received functions
|
||||||
|
|
||||||
|
* Use methods to proccess the matrices.
|
||||||
|
|
||||||
|
* Find root of functions throw method of bissection and method of newton
|
||||||
|
|
||||||
|
* Solve Diferential Equations throw method of euler and runge
|
||||||
|
|
||||||
|
* Performe Minimus Interpolation and Polinomial Interpolation
|
||||||
|
|
||||||
|
## Syntax
|
||||||
|
|
||||||
|
To initialize a Otter instance linked to functions use the following syntax `otr = Otter.algebra(f)`, where `otr` will be a arbitrary name for the instance and `f` is a function of *one variable*.
|
||||||
|
|
||||||
|
To initialize a Otter instance linked to data and interpolation use the following syntax `otr = Otter.interpolation(data)`, where `otr` will be a arbitrary name for the instance and data will be a *numpy* matrix where the first columns has to contain the values for `x` and the second column contains the values for `y`.
|
||||||
|
|
||||||
|
### Algebra
|
||||||
|
|
||||||
|
Algebra is a Python Class where some of the features described previously are defined as Classes as well, like: `Integral`, `Roots`, `EDO` (diferential equations).
|
||||||
|
|
||||||
|
#### Integral
|
||||||
|
|
||||||
|
To call the class *Integral* append the sufix with lower case in front of the instance like: `otr.integral`. The Integral class has two other class defined inside, `Simple` and `Double`, to call them append the sufix with lower case in front as `otr.integral.simple` or `otr.integral.double`. Then pick between Riemann's Method or Simpson's Method by appending the sufix `riemann` or `simpson` as well.
|
||||||
|
|
||||||
|
After that the syntax will be something like `otr.integral.double.riemann(a,b,c,d,n,m)`, where `a` and `c` will be the first value of the interval of integration respectively in x and y, `b` and `d` will be the last, `n` and `m` will be the number of partitions.
|
||||||
|
|
||||||
|
The syntax for one variable integrations will be `otr.integral.simple.riemann(a,b,n)`.
|
||||||
|
|
||||||
|
If `n` is not defined the standart value in 10^6 partitions for one variable and 10^4 for double. And if `m` is not defined the standart value will be equal to `n`.
|
||||||
|
|
||||||
|
#### Roots
|
||||||
|
|
||||||
|
To call the class *Root* append the sufix with lower case in front of the instance like: `otr.roots`. The Roots class has three methods defined inside, `bissec`, `newton` and `bissec_newton`, to call them append the sufix with lower case in front as `otr.roots.bissec` or `otr.roots.newton` or even `otr.roots.bissecnewton`.
|
||||||
|
|
||||||
|
The syntax for the bissection method and bissec_newton is equal to `otr.roots.bissec(a,b,e)` and `otr.roots.bissec_newton(a,b,e)`, where `a` is the first element of the interval containing the root and `b` is the last, `e` being the precision.
|
||||||
|
|
||||||
|
The syntax for the newton method is equal to `otr.roots.newton(a,e)`, where `a` is the element closest to the root and `e` is the precision.
|
||||||
|
|
||||||
|
If `e` is not defined the standart value is 10^(-6).
|
||||||
|
|
||||||
|
#### Diferential Equations
|
||||||
|
|
||||||
|
To call the class *EDO* (*E*quações *D*iferenciais *O*rdinárias) append the sufix with lower case in front of the instance like: `otr.edo`. The *EDO* class has two methods defined inside: `euler` and `runge`, to call them append the sufix with lower case in front as `otr.edo.euler` or `otr.edo.runge`.
|
||||||
|
|
||||||
|
The syntax for the diferential equations method is equal to `otr.edo.euler(a,y,b,n)` or `otr.edo.runge(a,y,b,n)`, where `a` and `y` will be the inintial point and `b` is the value in *x* which you want to know the corresponding value in *y* and `n` is the number of operations.
|
||||||
|
|
||||||
|
If `n` is not defined the standart value is 10^7.
|
||||||
|
|
||||||
|
### Interpolation
|
||||||
|
|
||||||
|
The python class *Interpolation* is divided in one method, minimus interpolation, and one class, polinomial interpolation.
|
||||||
|
|
||||||
|
To call the method *minimus* use a syntax like `otr = Otter.interpolation(data)`, where `otr` is an instance and append the method in front of the instance like: `otr.minimus(x)`, where *x* is value of *f(x)* you want to estimate.
|
||||||
|
|
||||||
|
To call the class *Polinomial* append the sufix with lower case in front of the instance like: `otr.polinomial`. The *Polinomial* class has four methods defined inside: `vandermonde`, `lagrange`, `newton` and `gregory`, to call them append the sufix with lower case in front like `otr.edo.gregory(x)` where *x* is value of *f(x)* you want to estimate.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
To install the package from source `cd` into the directory and run:
|
||||||
|
|
||||||
|
`pip install .`
|
||||||
|
|
||||||
|
or run
|
||||||
|
|
||||||
|
`pip install otter`
|
||||||
|
|
||||||
|
Platform: UNKNOWN
|
||||||
|
Classifier: Programming Language :: Python :: 3
|
||||||
|
Classifier: License :: OSI Approved :: GNU General Public License v2 (GPLv2)
|
||||||
|
Classifier: Operating System :: OS Independent
|
||||||
|
Classifier: Development Status :: 2 - Pre-Alpha
|
||||||
|
Requires-Python: >=3.6
|
||||||
|
Description-Content-Type: text/markdown
|
||||||
9
yoshi_otter.egg-info/SOURCES.txt
Normal file
9
yoshi_otter.egg-info/SOURCES.txt
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
README.md
|
||||||
|
setup.py
|
||||||
|
Otter/Otter.py
|
||||||
|
Otter/__init__.py
|
||||||
|
yoshi_otter.egg-info/PKG-INFO
|
||||||
|
yoshi_otter.egg-info/SOURCES.txt
|
||||||
|
yoshi_otter.egg-info/dependency_links.txt
|
||||||
|
yoshi_otter.egg-info/requires.txt
|
||||||
|
yoshi_otter.egg-info/top_level.txt
|
||||||
1
yoshi_otter.egg-info/dependency_links.txt
Normal file
1
yoshi_otter.egg-info/dependency_links.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
3
yoshi_otter.egg-info/requires.txt
Normal file
3
yoshi_otter.egg-info/requires.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
numpy
|
||||||
|
pandas
|
||||||
|
yoshi-seals
|
||||||
1
yoshi_otter.egg-info/top_level.txt
Normal file
1
yoshi_otter.egg-info/top_level.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Otter
|
||||||
Reference in New Issue
Block a user