数学
0、目录
高斯消元、FFT、数值方法(二分,三分,幸普森)、java大整数
1、高斯消元
const double eps=1e-8;
const int maxn=100+10;
typedef double Matrix[maxn][maxn];
//要求系数矩阵可逆
//A是增广矩阵,A[i][n]表示方程右边常系数
//运算结束后A[i][n]是第i个未知数的解
void gauss_elimination(Matrix A,int n){
int i,j,k,r;
for(i=0;i<n;i++){
r=i;
for(j=i+1;j<n;j++){
if(fabs(A[j][i])>fabs(A[r][i])) r=j;
}
if(r!=i) for(j=0;j<=n;j++) swap(A[r][j],A[i][j]);
for(k=i+1;k<n;k++){
double f=A[k][i]/A[i][i];
for(j=i;j<=n;j++) A[k][j]-=f*A[i][j];
}
}
//回代
for(i=n-1;i>=0;i--){
for(j=i+1;j<n;j++){
A[i][n]-=A[j][n]*A[i][j];
}
A[i][n]/=A[i][i];
}
}
//这种方法不用回代
void gauss_jordan(Matrix A,int n){
int i,j,k,r;
for(i=0;i<n;i++){
r=i;
for(j=i+1;j<n;j++){
if(fabs(A[j][i])>fbas(A[r][i])) r=j;
}
if(fabs(A[r][i])<eps) continue;
if(r!=i) for(j=0;j<=n;j++) swap(A[r][j],A[i][j]);
for(k=0;k<n;k++) if(k!=i){
for(j=n;j>=i;j--) A[k][j]-=A[k][i]/A[i][i]*A[i][j];
}
}
}
2、FFT
转自http://www.gatevin.moe/acm/fft%E7%AE%97%E6%B3%95%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/
struct Complex {
double real, image;
Complex(double real, double image):real(real),image(image) {}
Complex() {}
friend Complex operator + (const Complex &c1, const Complex &c2) {
return Complex(c1.real + c2.real, c1.image + c2.image);
}
friend Complex operator - (const Complex &c1, const Complex &c2) {
return Complex(c1.real - c2.real, c1.image - c2.image);
}
friend Complex operator * (const Complex &c1, const Complex &c2) {
return Complex(c1.real*c2.real - c1.image*c2.image, c1.real*c2.image + c1.image*c2.real);
}
}a[maxn];
struct IterativeFFT {
Complex A[maxn];
int rev(int id, int len) {
int ret = 0;
for(int i = 0; (1 << i) < len; i++) {
ret <<= 1;
if(id & (1 << i)) ret |= 1;
}
return ret;
}
//当DFT= 1时是DFT, DFT = -1则是逆DFT
//对长度为len(2的幂)的数组进行DFT变换
void FFT(Complex *a,int len, int DFT) {
for(int i = 0; i < len; i++)
A[rev(i, len)] = a[i];
for(int s = 1; (1 << s) <= len; s++) {
int m = (1 << s);
Complex wm = Complex(cos(DFT*2*PI/m), sin(DFT*2*PI/m));
//这一层结点的包含数组元素个数都是(1 << s)
for(int k = 0; k < len; k += m) {
Complex w = Complex(1, 0);
//折半引理, 根据两个子节点计算父亲节点
for(int j = 0; j < (m >> 1); j++) {
Complex t = w*A[k + j + (m >> 1)];
Complex u = A[k + j];
A[k + j] = u + t;
A[k + j + (m >> 1)] = u - t;
w = w*wm;
}
}
}
if(DFT == -1) for(int i = 0; i < len; i++) A[i].real /= len, A[i].image /= len;
for(int i=0; i<len; i++) a[i]=A[i];
}
void solve(){
Complex a[4];
a[0]=Complex(0,0);
a[1]=Complex(1,0);
a[2]=Complex(2,0);
a[3]=Complex(3,0);
FFT(a,4,1);
cout<<"----------After DFT----------"<<endl;
for(int i=0;i<4;i++) printf("%.3lf+%.3lfi\n",a[i].real,a[i].image);
FFT(a,4,-1);
cout<<"----------After DFT----------"<<endl;
for(int i=0;i<4;i++) printf("%.3lf+%.3lfi\n",a[i].real,a[i].image);
}
} myfft;
3、数值方法
3.1、二分
double l=0,r=1,m;
for(int i=0;i<100;i++){
m=l+(r-l)/2;
if(F(m)<0) r=m;
else l=m;
}
3.2、三分
double l=0,r=1000;
for(int i=0;i<100;i++){
double m1=l+(r-l)/3;
double m2=r-(r-l)/3;
if(F(m1)<F(m2)) r=m2; else l=m1;
}
3.3、辛普森公式
double F(double x){
return x*x;
}
double simpson(double l,double r){
double m=l+(r-l)/2;
return (F(l)+4*F(m)+F(r))*(r-l)/6;
}
double asr(double l,double r,double eps,double A){
double m=l+(r-l)/2;
double L=simpson(l,m),R=simpson(m,r);
if(fabs(L+R-A)<=15*eps) return L+R+(L+R-A)/15.0;
return asr(l,m,eps/2,L)+asr(m,r,eps/2,R);
}
double asr(double l,double r,double eps){
return asr(l,r,eps,simpson(l,r));
}
4、大数
import java.util.*;
import java.math.*;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
while (cin.hasNext()) {
BigInteger a, b, c, d;
a = cin.nextBigInteger();
b = cin.nextBigInteger();
c = BigInteger.valueOf(10);
d = BigInteger.valueOf(20);
// System.out.println(c);
// System.out.println(d);
// 四则运算
System.out.println(a.add(b));// 加
System.out.println(a.subtract(b));// 减
System.out.println(a.multiply(b));// 乘
System.out.println(a.divide(b));// 除
System.out.println(a.remainder(b));// 取模
// 比较
if (a.compareTo(b) == 0)
System.out.println("a==b");
else if (a.compareTo(b) > 0)
System.out.println("a>b");
else if (a.compareTo(b) < 0)
System.out.println("a<b");
// 十进制表示
System.out.println(a.toString());
// 返回大整数p进制的制服表示
int p = 8;
System.out.println(a.toString(p));
/*
// 赋值
BigDecimal d = new BigDecimal("-123456.31");
//取整 BigDecimal
x = new BigDecimal(2.3);
System.out.println(x.setScale(0, BigDecimal.ROUND_FLOOR));
System.out.println(x.setScale(0, BigDecimal.ROUND_CEILING));
*/
}
}
}
BigDecimal:http://blog.csdn.net/hurmishine/article/details/52312987
BigInteger:http://blog.csdn.net/hurmishine/article/details/52312902