📄 psn_linalg.c
字号:
/*psn_lib/psn_linalg.c*/
/***********************************************************************
Finite Volume Poisson PDE Solver: C-Library & Matlab Toolbox
Implements numerical solution of Poisson PDE
in 2D Cartesian and Cylindrical coordinates
Copyright (C) 2004 Igor Kaufman
Copyright (C) 2004 Lancaster University, UK
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Author's email: i.kaufman@lancaster.ac.uk
APPLICATION : COMMON ROUTINES
VERSION : 1.0
************************************************************************/
/*17.01.04*/
#include "psn.h"
/*
Function to invert tridiagonal matrix equation.
Left, centre, and right diagonal elements of matrix
stored in arrays a, b, c, respectively.
Right-hand side stored in array w.
Solution written to array u.
Matrix is NxN. Arrays a, b, c, w, u assumed to be of extent N+2,
with redundant 0 and N+1 elements.
Code source:http://farside.ph.utexas.edu/teaching/329/lectures/node77.html
*/
int psn_tridiag_solve_low(
const size_t N,
const PSN_VECTOR a,
PSN_VECTOR b,
const PSN_VECTOR c,
PSN_VECTOR u
)
{
size_t i,k;
if (b[1]==0) return 2;
for (i = 2,k=1; i <=N; i++,k++) {
double t = a[i]/b[k]; b[i]-= t*c[k]; u[i]-= t*u[k]; if (b[i]==0) return 2; } u[N]/= b[N]; for (i = N - 1;i>0; i--) { u[i]-=c[i]*u[i+1]; u[i]/=b[i]; } return 0;}
int psn_tridiag_solve_std(
const size_t N,
const PSN_VECTOR a,
const PSN_VECTOR b,
const PSN_VECTOR c,
const PSN_VECTOR w,
PSN_VECTOR u
)
{
size_t i;
int res;
PSN_VECTOR b1=psn_vector_create(N+2);
if (!b1) return 1;
for (i=0;i<N+1;i++) {
b1[i]=b[i];
u[i]=w[i];
}
res=psn_tridiag_solve_low(N,a,b1,c,u);
psn_vector_free(b1);
return res;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -