Skip to content

Commit 4516fae

Browse files
committed
version 0.7.5 release
1 parent 6b2647e commit 4516fae

10 files changed

+214
-5
lines changed

lib/mathalgorithm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// version 0.7.5 pre-release
1+
// version 0.7.5 release
22
#ifndef MATHALGORITHM_H
33
#define MATHALGORITHM_H
44
#include "numbertheory.h"

lib/numerical.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -658,17 +658,17 @@ template <class DD> DD D(std::function<DD(DD)> f,DD x,string str)
658658
{
659659
if(str=="center")
660660
{
661-
const DD h = 1e-7;
661+
const DD h = 1e-8;
662662
return (f(x + h) - f(x - h)) / (2 * h);
663663
}
664664
if(str=="forward")
665665
{
666-
const DD h = 1e-7;
666+
const DD h = 1e-8;
667667
return (f(x + h) - f(x))/h;
668668
}
669669
if(str=="backward")
670670
{
671-
const DD h = 1e-7;
671+
const DD h = 1e-8;
672672
return (f(x) - f(x-h))/h;
673673
}
674674
cerr << "错误:没有定义该方法。" << '\n';
@@ -699,7 +699,7 @@ template<class DD> DD D(std::function<DD(Matrix<DD>)>f,int n,const Matrix<DD> &x
699699
cerr << "错误:求偏导的分量越界" << '\n';
700700
return f(x);
701701
}
702-
const DD h=1e-7;
702+
const DD h=1e-8;
703703
auto y=x;
704704
y(n,0)=Get(x,n,0)+h;
705705
return (f(y) - f(x)) / h;

src/hpc and nbt example.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <iostream>
2+
#include <ctime>
3+
#include <chrono>
4+
#include "../lib/mathalgorithm.h"
5+
using namespace mal;
6+
7+
/* minimal inclusion:
8+
#include "../lib/highprecision.h"
9+
#include "../lib/numbertheory.h"
10+
using namespace hpc;
11+
using namespace nbt; */
12+
13+
int main()
14+
{
15+
auto start = std::chrono::system_clock::now();
16+
BigInt a("23948576235");
17+
BigInt b("823761298");
18+
BigInt c("2");
19+
20+
BigInt m("986732000000000000000123761");
21+
BigInt n("5000000009");
22+
BigInt x = Fibonacci(1000000);
23+
BigInt y = Fibonacci(100000);
24+
25+
std::cout << "a * b = " << a*b << std::endl;
26+
std::cout << "c ^ 100 = " << (c^100) << std::endl;
27+
std::cout << "Prime test for m: " << PrimeQ(m) << std::endl;
28+
std::cout << "Find a factor of n: " << FindAFactor(n) << std::endl;
29+
std::cout << "integer length of x is " << IntegerLength(x) << std::endl;
30+
std::cout << "x mod y is " << x % y << std::endl;
31+
std::cout << "log2 of a is " << log2(a) << std::endl;
32+
auto end = std::chrono::system_clock::now();
33+
std::cout << "total time=" << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()
34+
<< "ms"<<std::endl;
35+
system("pause");
36+
return 0;
37+
}
38+

src/lag example.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
#include "../lib/mathalgorithm.h"
3+
using namespace mal;
4+
5+
/* minimal inclusion:
6+
#include "../lib/linearalgebra.h"
7+
using namespace lag; */
8+
9+
int main()
10+
{
11+
Matrix<double> A({{1,1,-1},{1,2,-2},{-2,1,1}},3,3);
12+
Matrix<double> b({{1}, {0}, {1}}, 3, 1);
13+
std::cout << LinearSolve(A, b) <<std::endl;
14+
system("pause");
15+
return 0;
16+
}

src/nmr example.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <iostream>
2+
#include "../lib/mathalgorithm.h"
3+
using namespace mal;
4+
5+
/* minimal inclusion:
6+
#include "../lib/numerical.h"
7+
using namespace nmr; */
8+
9+
int main()
10+
{
11+
Matrix<double> base(
12+
{{0.4,-0.916},{0.5,-0.693},{0.8,-0.223},{0.9,-0.105}});
13+
Matrix<double> data({{0.6},{0.7}},2,1);
14+
Matrix<double> result=Interpolation(base,data);
15+
std::cout<<result<<std::endl;
16+
17+
auto p = [](double x) { return 4.0 / (1 + x * x); };
18+
auto q = Integrate<double>(p, 0, 1);
19+
N(q, 10);
20+
21+
auto f=[](double x){ return x*x*exp(x); };
22+
auto g=D<double>(2,f);
23+
std::cout<<g(0)<<std::endl;
24+
25+
system("pause");
26+
return 0;
27+
}

src/ode example.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
#include "../lib/mathalgorithm.h"
3+
using namespace mal;
4+
5+
/* minimal inclusion:
6+
#include "../lib/ode.h"
7+
using namespace ode; */
8+
9+
int main()
10+
{
11+
function<double(double, double)> f = [](double x, double y) { return y; };
12+
std::cout << DSolve(f, {0., 1.}, 1.) << std::endl;
13+
system("pause");
14+
return 0;
15+
}

src/opt example.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <iostream>
2+
#include "../lib/mathalgorithm.h"
3+
using namespace mal;
4+
5+
/* minimal inclusion:
6+
#include "../lib/optimization.h"
7+
using namespace opt; */
8+
9+
int main()
10+
{
11+
double epsilon = 1e-6;
12+
13+
std::cout << "约束优化问题:" << std::endl;
14+
function<double(double,double)> f_pre = [](double x, double y)
15+
{
16+
return x + sqrt(3.) * y;
17+
};
18+
auto f = MatrizeInputs(f_pre);
19+
function<Matrix<double>(double,double)> c_pre = [](double x, double y)
20+
{
21+
return Matrix<double>(x*x+y*y-1.,1,1);
22+
};
23+
auto c = MatrizeInputs(c_pre);
24+
Matrix<double> initial(1.,2,1);
25+
std::cout << "AugmentedLagrangian"<<std::endl<<
26+
Transpose(AugmentedLagrangian<double>(f, c, initial, epsilon, BFGS<double>))
27+
<<std::endl;
28+
29+
std::cout << "无约束优化问题:" << std::endl;
30+
function<double(double,double)> g_pre = [](double x,double y)
31+
{
32+
return x * x + 10. * y * y;
33+
};
34+
auto g = MatrizeInputs(g_pre);
35+
Matrix<double> initial2(vector<vector<double>>({{-10.}, {-1.}}));
36+
double step = 0.085;
37+
std::cout << "GradientDescentFixedStepsize"<<std::endl<<
38+
Transpose(GradientDescentFixedStepsize(g, initial2, epsilon, step)) << std::endl;
39+
std::cout << "GradientDescentWolfe"<<std::endl<<
40+
Transpose(GradientDescentWolfe(g, initial2, epsilon)) << std::endl;
41+
std::cout << "GradientDescentGoldstein"<<std::endl<<
42+
Transpose(GradientDescentGoldstein(g, initial2, epsilon)) << std::endl;
43+
std::cout << "BarzilarBorwein"<<std::endl<<
44+
Transpose(BarzilarBorwein(g, initial2, epsilon)) << std::endl;
45+
std::cout << "FletcherReeves" << std::endl<<
46+
Transpose(FletcherReeves(g, initial2, epsilon)) << std::endl;
47+
std::cout << "PolakRibiere" << std::endl<<
48+
Transpose(PolakRibiere(g, initial2, epsilon)) << std::endl;
49+
std::cout << "SR1"<<std::endl<<
50+
Transpose(SR1(g, initial2, epsilon)) << std::endl;
51+
std::cout << "DFP"<<std::endl<<
52+
Transpose(DFP(g, initial2, epsilon)) << std::endl;
53+
std::cout << "BFGS"<<std::endl<<
54+
Transpose(BFGS(g, initial2, epsilon)) << std::endl;
55+
56+
Matrix<double> initial3(vector<vector<double>>({{-0.1}, {-0.1}}));
57+
std::cout << "ClassicNewton"<<std::endl<<
58+
Transpose(ClassicNewton(g, initial3, epsilon)) << std::endl;
59+
std::cout << "ModifiedNewtonGoldstein"<<std::endl<<
60+
Transpose(ModifiedNewtonGoldstein(g, initial2, epsilon)) << std::endl;
61+
system("pause");
62+
return 0;
63+
}
64+

src/plt example.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <iostream>
2+
#include "../lib/mathalgorithm.h"
3+
using namespace mal;
4+
5+
/* minimal inclusion:
6+
#include "../lib/plot.h"
7+
using namespace plt; */
8+
9+
int main()
10+
{
11+
function<double(double)> f = [](double x)
12+
{ return sin(x); };
13+
Plot<double>(f, vector<double>({0., 2*Pi<double>}));
14+
system("pause");
15+
return 0;
16+
}
17+

src/ply example.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <iostream>
2+
#include "../lib/mathalgorithm.h"
3+
using namespace mal;
4+
5+
/* minimal inclusion:
6+
#include "../lib/polynomial.h"
7+
using namespace ply; */
8+
9+
int main()
10+
{
11+
Polynomial<int> f({1,1,1},2);
12+
Polynomial<int> g({5,-3,0,1},3);
13+
Polynomial<int> h=f*g;
14+
std::cout<<"h(x)="<<h<<std::endl;
15+
std::cout<<"h(5)="<<h(5)<<std::endl;
16+
system("pause");
17+
return 0;
18+
}

src/prb example.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <iostream>
2+
#include "../lib/mathalgorithm.h"
3+
using namespace mal;
4+
5+
/* minimal inclusion:
6+
#include "../lib/probability.h"
7+
using namespace prb; */
8+
9+
int main()
10+
{
11+
std::cout << CDF(NormalDistribution(1, 1), 1) << std::endl;
12+
system("pause");
13+
return 0;
14+
}

0 commit comments

Comments
 (0)