Skip to content

Commit

Permalink
Added: initial packages [V2023.10.1]
Browse files Browse the repository at this point in the history
  • Loading branch information
mandal-anik10 committed Oct 3, 2023
1 parent 254f051 commit 9d408f3
Show file tree
Hide file tree
Showing 15 changed files with 624 additions and 2 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
# unipy
A unified module for doing undergrad-grad level coding in physics using python.
# **[unipy](https://github.com/mandal-anik10/unipy)**
___________________________________________________________
A unified module for numerical calculations in physics using python.

I have a dream of making a numerical universe where I can simulate nature and physical phenomena just by incorporating the most fundamental law/infromation `(if, we will find the one most fundamental theroy of the universe)` of physics. Thhis module is in developement for that purpose. If you want to contribute, please send a mail. you can find contact information [here](https://mandal-anik10.github.io).

Checkout the [documantation](https://mandal-anik10.github.io/unipy/) for more information about this module.
___________________________________________________________
## Installation:
- Clone the repository in your computer and move to the repository:
```
$ git clone https://github.com/mandal-anik10/unipy.git
$ cd unipy
```
- Now you should have a shell prompt ready in the `unipy` folder, and by running `$ ls` you should see a folder again called `unipy`. You can then install `unipy` with:
```
$ python setup.py install
```
___________________________________________________________
38 changes: 38 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import setuptools

with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()

setuptools.setup(
name="unipy",
version="2023.10.1",
author="Anik Mandal",
author_email="mandal.anik10@gmail.com",
description="A unified module for physics",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/mandal-anik10/unipy",
project_urls={
"Unipy webpage": "https://mandal-anik10.github.io/unipy/",
},
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
install_requires=[
"numpy",
"sklearn"
],
package_dir={},
packages=setuptools.find_packages(where="src"),
python_requires=">=3.6"
)








3 changes: 3 additions & 0 deletions unipy/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions unipy/.idea/LocalModule.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions unipy/.idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions unipy/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions unipy/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

169 changes: 169 additions & 0 deletions unipy/BasicOp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
"""
Module for basic mathematical operations and physical quantities.
Mandal, Anik | 2023-10-03
"""


from numpy import *

# Mathematical Operations----------

# Summation:
def Sum(arr, step = 1):
'''
Returns sum of the elements in a input list
-----Inputs----------
arr : list : list of integers/float
step : int : increment in the index value
-----Output----------
s : int/float : sum of the elements of list
* Equivalant to numpy.sum()
'''
s = 0
for i in range(0, len(arr), step):
s = s + arr[i]
return s


# Factorial:
def Fact(n, step=1):
'''
Returns factorial of a number
-----Inputs----------
n : int : Input number
step : int : difference between two successive numbers
-----Output----------
f : int : factorial of the number
'''
f = 1
for i in range(1, n+1, step):
f = f * i
return f

# Integration:
def Integrate(y_data, x_data, spacing='linear'):
'''
Function for performing integration using trapizoidal method.
-----Inputs----------
y_data : list : a list of numbers defing the y values of the function
x_data : list : a list of numbers defing the x values of the function
spacing : string : specification of increment vector/scaler
- default :'linear'
-----Output----------
s : float : value of the integration
'''
if len(y_data) != len(x_data) or len(x_data) <= 1 or len(y_data) <= 1:
raise ValueError("x and y data are not consistant for trapizoidal integration")
else:
if spacing == 'linear':
h = x_data[1] - x_data[0]
s = Sum(y_data[1:len(x_data)-1])
s = (y_data[0] + y_data[-1] + 2 * s) * h / 2
else:
s = 0
for i in range(len(x_data)-1):
h = x_data[i+1] - x_data[i]
s = s + h * (y_data[i] + y_data[i+1]) / 2
return s

def Integrate_simp(y_data, x_data, spacing='linear'):
'''
Function for performing integration using Simpson's 1/3 method.
-----Inputs----------
y_data : list : a list of numbers defing the y values of the function
x_data : list : a list of numbers defing the x values of the function
spacing : string : specification of increment vector/scaler
- default :'linear'
-----Output----------
s : float : value of the integration
'''
if len(y_data) != len(y_data) or len(x_data) <= 2 or len(y_data) <= 2:
raise ValueError("x and y data are not consistant for simpson integration")
else:
if spacing == 'linear':
h = x_data[1] - x_data[0]
s = 4 * Sum(y_data[1, len(x_data)-1], 2)
s = s + 2 * Sum(y_data[2, len(x_data)-1], 2)
s = (y_data[0] + s + y_data[-1]) * h / 3
else:
raise NotImplementedError('Simpson\'s 1/3 integration is not implemented yet'+\
'in nonlinear scale; use tapizoidal integration.')

# Fourier Series:
def FourierSeries(y_data, x_data, nc=10):
'''
Returns Fouries Series of a function
-----Inputs----------
y_data : list : a list of numbers defing the y values of the function
x_data : list : a list of numbers defing the x values of the function
nc : int : number of coefficents of sin and cos series
- default :10
-----Outputs----------
cc : list : coeff. of cosine series
cs : list : coeff. of sin series
y_new : list : fourier function
'''
cc, cs, y_new = [], [], []

for i in range(nc):
yc = y_data * cos(pi * i * x_data)
ys = y_data * sin(pi * i * x_data)

sc = Integrate(yc, x_data)
ss = Integrate(ys, x_data)

cc.append(sc)
cs.append(ss)

cc[0] = cc[0]/2

for i in range(len(x_data)):
s = 0
for j in range(nc):
s = s + cc[j] * cos(pi * j *x_data[i]) + cs[j] * sin(pi * j * x_data[i])

y_new.append(s)
return cc, cs, y_new


# Physical Quantities----------

# Inertia Tensor:
def Inertia_T(Mass_List, Pos_Tensor):
'''
Returns Inertia Tensor of discrete masses
-----Inputs----------
arr : list : list of integers/float
-----Output----------
s : int/float : sum of the elements of list
'''
I = np.zeros((3, 3))

for m in range(3):
for n in range(3):

if m == n:
for k in range(len(Mass_List)):
s = 0
for i in range(3):
if i != m:
s = s + Mass_List[k]*Pos_Tensor[k][i]**2

I[m][n] = I[m][n] + s

else:
for k in range(len(Mass_List)):
s = - Mass_List[k]*Pos_Tensor[k][m]*Pos_Tensor[k][n]

I[m][n] = I[m][n] + s
return I


75 changes: 75 additions & 0 deletions unipy/CurveFit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""
Module for curve fitting.
Mandal, Anik | 2023-10-03
"""

import numpy as np
from sklearn.linear_model import Lasso
from sklearn.preprocessing import PolynomialFeatures


# Linear Fit Module:
def LinearFit(x_data, y_Data):
'''
Returns slope and y-intersect of the best linear fit using least
square fit
-----Inputs----------
x_data : list : x coordinate of the input values
y_data : list : y coordinate of the input values
-----Outputs----------
m : float : slope of the best fit line
c : float : y-intersect of the best fit line.
'''
(sx, sy, p1, p2) = (0, 0, 0, 0)
for i in range(len(x_data)):
sx = sx + x_data[i]
sy = sy + y_Data[i]
xb = sx/len(x_data)
yb = sy/len(y_Data)
for i in range(len(x_data)):
p1 = p1 + (x_data[i]-xb)*(y_Data[i]-yb)
p2 = p2 + (x_data[i]-xb)**2
m = p1/p2
c = yb - m*xb
return m, c


# Nolinear Fit Module:
def LassoRegression(x_data, y_data, max_degrees=10, alpha_value=0.2, num_points=int(1e5)):
'''
Returns data set of best fit curve using sklearn Lasso Regression
-----Inputs----------
x_data : list : x coordinates of the input values
y_data : list : y coordinates of the input values
max_degree : int : maximum degree for interpolation
- default : 10
alpha_value : float : defines alpha parameter for Lasso regression
- default : 0.2
num_points : int : number of point for interpolation
- default : 1e5
-----Outputs----------
x_new : list : x coordinates of the fitted curve
y_new : list : y coordinates of the fitted curve
'''
d = max_degrees
poly = PolynomialFeatures(degree=d, include_bias=False)
x_new = poly.fit_transform(x_data)

alp = alpha_value
sl = Lasso(alpha=alp).fit(x_new, y_data)

m = sl.coef_
c = sl.intercept_
s = sl.score(x_new, y_data)

x_new = np.linspace(min(x_data), max(x_data), num_points)
y_new = []
for i in range(len(x_new)):
a = 0
for j in range(len(m)):
a = a + m[j] * x_new[i] ** (j + 1)
y_new.append(a + c[0])
return x_new, y_new
22 changes: 22 additions & 0 deletions unipy/GraphPlot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
Module for graph plotting.
Mandal, Anik | 2023-10-03
"""

def ExpData2GraphData(x_data, y_data, small_x, small_y):
'''
Returns sum of the elements in a input list
-----Inputs----------
arr : list : list of integers/float
step : int : increment in the index value
-----Output----------
s : int/float : sum of the elements of list
'''
x_new = []
y_new = []
for i in range(len(x_data)):
x_new.append(x_data[i]/small_x)
y_new.append(y_data[i]/small_y)
return x_new, y_new
Loading

0 comments on commit 9d408f3

Please sign in to comment.