Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SciComputeKit - English

A Python scientific computing toolkit for matrix calculations and ordinary differential equation (ODE) solving. This project contains modules for linear algebra operations and numerical ODE solving, aiming to provide a clean interface for research and engineering computations.

Features

  • Linear System Solver: Uses NumPy's numpy.linalg.solve method to solve linear systems of the form A * x = b.
  • Eigenvalue and Eigenvector Analysis: Uses NumPy's numpy.linalg.eig function to compute eigenvalues and corresponding eigenvectors of square matrices.
  • Common Matrix Operations: Provides interfaces for matrix multiplication, transpose, trace (sum of diagonal elements), matrix inverse, and other operations based on NumPy.
  • Ordinary Differential Equation Solver: Based on SciPy's Runge-Kutta method (scipy.integrate.solve_ivp), solves initial value problems for ODEs, handling both single equations and coupled systems.

Installation

  1. Clone the repository:
    Use Git to clone the project code locally:
    git clone https://github.com/RYZENNAVI/SciComputeKit.git
    cd SciComputeKit
  2. Install dependencies:
    Python 3 environment is recommended. Install required dependencies:
    pip install -r requirements.txt
    This project depends on scientific computing libraries such as NumPy and SciPy.

Usage Examples

After installation, you can import core.py to use the provided functions. Here are some basic examples:

>>> import numpy as np
>>> from core import solve_linear_system, eigen_analysis, trace_matrix, inverse_matrix
>>> A = np.array([[1, 2], [3, 4]], dtype=float)
>>> b = np.array([5, 11], dtype=float)
>>> x = solve_linear_system(A, b)
>>> print(x)  # solution x should be [1. 2.]
[1. 2.]
>>> B = np.array([[2, 0], [0, 3]], dtype=float)
>>> eigvals, eigvecs = eigen_analysis(B)
>>> print(eigvals)  # eigenvalues of B
[2. 3.]
>>> print(eigvecs)  # eigenvectors of B (each column corresponds to one eigenvector)
[[1. 0.]
 [0. 1.]]
>>> print(trace_matrix(A))  # trace of A = 1 + 4
5.0
>>> G = np.array([[4, 7], [2, 6]], dtype=float)
>>> invG = inverse_matrix(G)
>>> print(invG)  # inverse of G
[[ 0.6 -0.7]
 [-0.2  0.4]]

As shown above, solve_linear_system returns the solution vector of the linear system, eigen_analysis returns eigenvalues and eigenvectors, trace_matrix returns the trace of the matrix, and inverse_matrix returns the matrix inverse. If the matrix is non-invertible or singular, the underlying numpy.linalg will raise an exception.

Below is an example of solving an ODE system using the Runge-Kutta method. We take a harmonic oscillator as an example, with equations $y_1' = y_2,; y_2' = -y_1$ and initial conditions $y_1(0)=1,; y_2(0)=0$ (the analytical solution is $y_1=\cos t,; y_2=-\sin t$):

>>> from core import solve_ode
>>> def f(t, y):
...     return [y[1], -y[0]]  # define the ODE system
>>> sol = solve_ode(f, (0, 6.28), [1, 0], t_eval=[0, 3.14, 6.28])
>>> print(sol.t)          # time points for the integration
[0.   3.14 6.28]
>>> print(np.round(sol.y, 2))  # approximate solutions (y1 and y2) at each time point
[[ 1.   -1.    1.  ]
 [ 0.    0.   -0.  ]]

From the output, we can see: at $t=0$, $(y_1,y_2)=(1,0)$; at $t \approx 3.14$ (i.e., $\pi$), $(y_1,y_2)\approx(-1, 0)$; at $t \approx 6.28$ (i.e., $2\pi$), $(y_1,y_2)\approx(1, 0)$, which matches the expected cosine/sine function solution.

For more example code, please refer to examples.py. Run python examples.py to see the complete demonstration.

Testing

This project provides basic unit tests to ensure the correctness of core functions. Run python tests.py to execute the tests and verify that matrix operations and ODE solving results meet expectations.


SciComputeKit - 中文

一个用于矩阵计算和常微分方程求解的 Python 科学计算工具包。该项目包含线性代数运算和常微分方程 (ODE) 数值求解等功能模块,旨在提供简洁的接口,方便科研和工程计算使用。

功能模块

  • 线性方程组求解:使用 NumPy 的 numpy.linalg.solve 方法,求解形如 A * x = b 的线性方程组。
  • 特征值与特征向量分析:使用 NumPy 的 numpy.linalg.eig 函数,计算方阵的特征值和对应的特征向量。
  • 常见矩阵操作:提供矩阵乘法、转置、迹(主对角线元素之和)、逆矩阵等运算接口,基于 NumPy 实现。
  • 常微分方程组求解:基于 SciPy 提供的 Runge-Kutta 方法(scipy.integrate.solve_ivp),求解常微分方程初值问题,可处理单个方程或耦合方程组。

安装

  1. 克隆仓库:
    使用 Git 将项目代码克隆到本地:
    git clone https://github.com/RYZENNAVI/SciComputeKit.git
    cd SciComputeKit
  2. 安装依赖:
    建议使用 Python 3 环境。安装所需依赖库:
    pip install -r requirements.txt
    该项目依赖于 NumPy 和 SciPy 等科学计算库。

使用示例

安装完成后,可以通过导入 core.py 来使用提供的功能。以下是一些基本示例:

>>> import numpy as np
>>> from core import solve_linear_system, eigen_analysis, trace_matrix, inverse_matrix
>>> A = np.array([[1, 2], [3, 4]], dtype=float)
>>> b = np.array([5, 11], dtype=float)
>>> x = solve_linear_system(A, b)
>>> print(x)  # 解 x 应为 [1. 2.]
[1. 2.]
>>> B = np.array([[2, 0], [0, 3]], dtype=float)
>>> eigvals, eigvecs = eigen_analysis(B)
>>> print(eigvals)  # B 的特征值
[2. 3.]
>>> print(eigvecs)  # B 的特征向量(每列对应一个特征向量)
[[1. 0.]
 [0. 1.]]
>>> print(trace_matrix(A))  # A 的迹 = 1 + 4
5.0
>>> G = np.array([[4, 7], [2, 6]], dtype=float)
>>> invG = inverse_matrix(G)
>>> print(invG)  # G 的逆矩阵
[[ 0.6 -0.7]
 [-0.2  0.4]]

如上所示,solve_linear_system 返回线性方程组的解向量,eigen_analysis 返回特征值和特征向量,trace_matrix 返回矩阵的迹,inverse_matrix 返回矩阵的逆。如果矩阵不可逆或奇异,底层 numpy.linalg 会抛出异常。

下面是使用 Runge-Kutta 方法求解常微分方程组的示例。我们以简谐振荡器为例,方程为 $y_1' = y_2,; y_2' = -y_1$,初始条件 $y_1(0)=1,; y_2(0)=0$(其解析解为 $y_1=\cos t,; y_2=-\sin t$):

>>> from core import solve_ode
>>> def f(t, y):
...     return [y[1], -y[0]]  # 定义微分方程组
>>> sol = solve_ode(f, (0, 6.28), [1, 0], t_eval=[0, 3.14, 6.28])
>>> print(sol.t)          # 积分计算的时间点
[0.   3.14 6.28]
>>> print(np.round(sol.y, 2))  # 各时间点对应的近似解(y1 和 y2)
[[ 1.   -1.    1.  ]
 [ 0.    0.   -0.  ]]

由输出结果可见:在 $t=0$ 时 $(y_1,y_2)=(1,0)$,在 $t \approx 3.14$(即 $\pi$)时 $(y_1,y_2)\approx(-1, 0)$,在 $t \approx 6.28$(即 $2\pi$)时 $(y_1,y_2)\approx(1, 0)$,与预期的余弦/正弦函数解相符。

更多示例代码请参见 examples.py,运行 python examples.py 可以查看完整演示。

测试

本项目提供了基本的单元测试以确保核心函数的正确性。运行 python tests.py 即可执行测试,验证矩阵运算和微分方程求解的结果是否符合预期。

About

A modular Python library for solving linear systems, eigenvalue analysis, and ODEs using NumPy and SciPy.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages