├── .gitattributes ├── .gitignore ├── MatrixFunction.sln ├── MatrixFunction ├── MatrixFunction.vcxproj ├── MatrixFunction.vcxproj.filters ├── bfgs-impl.h ├── bfgs.h ├── bfgs_test.cpp ├── ccholesky-impl.h ├── ccholesky.h ├── ccholesky_test.cpp ├── cevd-impl.h ├── cevd.h ├── cevd_test.cpp ├── cholesky-impl.h ├── cholesky.h ├── cholesky_test.cpp ├── conjgrad-impl.h ├── conjgrad.h ├── conjgrad_test.cpp ├── constants.h ├── cqrd-impl.h ├── cqrd.h ├── cqrd_test.cpp ├── csvd-impl.h ├── csvd.h ├── csvd_test.cpp ├── evd-impl.h ├── evd.h ├── evd_test.cpp ├── fft-impl.h ├── fft.h ├── fft_test.cpp ├── fftmr-impl.h ├── fftmr.h ├── fftmr_test.cpp ├── fftpf-impl.h ├── fftpf.h ├── fftpf_test.cpp ├── fitcurves.h ├── integral-impl.h ├── integral.h ├── integral_test.cpp ├── integrand.h ├── interpolation.h ├── inverse-impl.h ├── inverse.h ├── inverse_test.cpp ├── linequs1-impl.h ├── linequs1.h ├── linequs1_test.cpp ├── linequs2-impl.h ├── linequs2.h ├── linequs2_test.cpp ├── linequs3-impl.h ├── linequs3.h ├── linequs3_test.cpp ├── linesearch-impl.h ├── linesearch.h ├── lsfit_test.cpp ├── lsfitting-impl.h ├── lsfitting.h ├── lud-impl.h ├── lud.h ├── lud_test.cpp ├── main_test.cpp ├── matrix-impl.h ├── matrix.h ├── matrix_test.cpp ├── matrixmath-impl.h ├── matrixmath.h ├── matrixmath_test.cpp ├── newtoninterp-impl.h ├── newtoninterp.h ├── newtoninterp_test.cpp ├── nleroot-impl.h ├── nleroot.h ├── nleroot_test.cpp ├── nleroots-impl.h ├── nleroots.h ├── nleroots_test.cpp ├── nlfunc.h ├── nlfuncs.h ├── objfunc.h ├── pseudoinverse-impl.h ├── pseudoinverse.h ├── pseudoinverse_test.cpp ├── qrd-impl.h ├── qrd.h ├── qrd_test.cpp ├── spline3interp-impl.h ├── spline3interp.h ├── spline3interp_test.cpp ├── steepdesc-impl.h ├── steepdesc.h ├── steepdesc_test.cpp ├── svd-impl.h ├── svd.h ├── svd_test.cpp ├── usingdeclare.h ├── utilities-impl.h ├── utilities.h ├── utilities_test.cpp ├── vector-impl.h ├── vector.h ├── vector_test.cpp ├── vectormath-impl.h ├── vectormath.h └── vectormath_test.cpp └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.sln.docstates 8 | 9 | # Build results 10 | 11 | [Dd]ebug/ 12 | [Rr]elease/ 13 | x64/ 14 | build/ 15 | [Bb]in/ 16 | [Oo]bj/ 17 | 18 | # Enable "build/" folder in the NuGet Packages folder since NuGet packages use it for MSBuild targets 19 | !packages/*/build/ 20 | 21 | # MSTest test Results 22 | [Tt]est[Rr]esult*/ 23 | [Bb]uild[Ll]og.* 24 | 25 | *_i.c 26 | *_p.c 27 | *.ilk 28 | *.meta 29 | *.obj 30 | *.pch 31 | *.pdb 32 | *.pgc 33 | *.pgd 34 | *.rsp 35 | *.sbr 36 | *.tlb 37 | *.tli 38 | *.tlh 39 | *.tmp 40 | *.tmp_proj 41 | *.log 42 | *.vspscc 43 | *.vssscc 44 | .builds 45 | *.pidb 46 | *.log 47 | *.scc 48 | 49 | # Visual C++ cache files 50 | ipch/ 51 | *.aps 52 | *.ncb 53 | *.opensdf 54 | *.sdf 55 | *.cachefile 56 | 57 | # Visual Studio profiler 58 | *.psess 59 | *.vsp 60 | *.vspx 61 | 62 | # Guidance Automation Toolkit 63 | *.gpState 64 | 65 | # ReSharper is a .NET coding add-in 66 | _ReSharper*/ 67 | *.[Rr]e[Ss]harper 68 | 69 | # TeamCity is a build add-in 70 | _TeamCity* 71 | 72 | # DotCover is a Code Coverage Tool 73 | *.dotCover 74 | 75 | # NCrunch 76 | *.ncrunch* 77 | .*crunch*.local.xml 78 | 79 | # Installshield output folder 80 | [Ee]xpress/ 81 | 82 | # DocProject is a documentation generator add-in 83 | DocProject/buildhelp/ 84 | DocProject/Help/*.HxT 85 | DocProject/Help/*.HxC 86 | DocProject/Help/*.hhc 87 | DocProject/Help/*.hhk 88 | DocProject/Help/*.hhp 89 | DocProject/Help/Html2 90 | DocProject/Help/html 91 | 92 | # Click-Once directory 93 | publish/ 94 | 95 | # Publish Web Output 96 | *.Publish.xml 97 | 98 | # NuGet Packages Directory 99 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 100 | #packages/ 101 | 102 | # Windows Azure Build Output 103 | csx 104 | *.build.csdef 105 | 106 | # Windows Store app package directory 107 | AppPackages/ 108 | 109 | # Others 110 | sql/ 111 | *.Cache 112 | ClientBin/ 113 | [Ss]tyle[Cc]op.* 114 | ~$* 115 | *~ 116 | *.dbmdl 117 | *.[Pp]ublish.xml 118 | *.pfx 119 | *.publishsettings 120 | 121 | # RIA/Silverlight projects 122 | Generated_Code/ 123 | 124 | # Backup & report files from converting an old project file to a newer 125 | # Visual Studio version. Backup files are not needed, because we have git ;-) 126 | _UpgradeReport_Files/ 127 | Backup*/ 128 | UpgradeLog*.XML 129 | UpgradeLog*.htm 130 | 131 | # SQL Server files 132 | App_Data/*.mdf 133 | App_Data/*.ldf 134 | 135 | 136 | #LightSwitch generated files 137 | GeneratedArtifacts/ 138 | _Pvt_Extensions/ 139 | ModelManifest.xml 140 | 141 | # ========================= 142 | # Windows detritus 143 | # ========================= 144 | 145 | # Windows image file caches 146 | Thumbs.db 147 | ehthumbs.db 148 | 149 | # Folder config file 150 | Desktop.ini 151 | 152 | # Recycle Bin used on file shares 153 | $RECYCLE.BIN/ 154 | 155 | # Mac desktop service store files 156 | .DS_Store 157 | -------------------------------------------------------------------------------- /MatrixFunction.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.21005.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MatrixFunction", "MatrixFunction\MatrixFunction.vcxproj", "{6A0A9D75-BFF9-4683-AA83-205101120C95}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Win32 = Debug|Win32 11 | Release|Win32 = Release|Win32 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {6A0A9D75-BFF9-4683-AA83-205101120C95}.Debug|Win32.ActiveCfg = Debug|Win32 15 | {6A0A9D75-BFF9-4683-AA83-205101120C95}.Debug|Win32.Build.0 = Debug|Win32 16 | {6A0A9D75-BFF9-4683-AA83-205101120C95}.Release|Win32.ActiveCfg = Release|Win32 17 | {6A0A9D75-BFF9-4683-AA83-205101120C95}.Release|Win32.Build.0 = Release|Win32 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /MatrixFunction/bfgs-impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * bfgs-impl.h 28 | * 29 | * Implementation for BFGS class. 30 | * 31 | * Zhang Ming, 2010-03, Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | /** 36 | * constructors and destructor 37 | */ 38 | template 39 | BFGS::BFGS() : LineSearch() 40 | { 41 | } 42 | 43 | template 44 | BFGS::~BFGS() 45 | { 46 | } 47 | 48 | 49 | /** 50 | * Finding the optimal solution. The default tolerance error and maximum 51 | * iteratin number are "tol=1.0e-6" and "maxItr=100", respectively. 52 | */ 53 | template 54 | void BFGS::optimize(Ftype &func, Vector &x0, 55 | Dtype tol, int maxItr) 56 | { 57 | // initialize parameters. 58 | int k = 0, 59 | cnt = 0, 60 | N = x0.dim(); 61 | 62 | Dtype ys, 63 | yHy, 64 | alpha; 65 | Vector d(N), 66 | s(N), 67 | y(N), 68 | v(N), 69 | Hy(N), 70 | gPrev(N); 71 | Matrix H = eye(N, Dtype(1.0)); 72 | 73 | Vector x(x0); 74 | Dtype fx = func(x); 75 | this->funcNum++; 76 | Vector gnorm(maxItr); 77 | Vector g = func.grad(x); 78 | gnorm[k++] = norm(g); 79 | 80 | while ((gnorm(k) > tol) && (k < maxItr)) 81 | { 82 | // descent direction 83 | d = -H * g; 84 | 85 | // one dimension searching 86 | alpha = this->getStep(func, x, d); 87 | 88 | // check flag for restart 89 | if (!this->success) 90 | if (norm(H - eye(N, Dtype(1.0))) < EPS) 91 | break; 92 | else 93 | { 94 | H = eye(N, Dtype(1.0)); 95 | cnt++; 96 | if (cnt == maxItr) 97 | break; 98 | } 99 | else 100 | { 101 | // update 102 | s = alpha * d; 103 | x += s; 104 | fx = func(x); 105 | this->funcNum++; 106 | gPrev = g; 107 | g = func.grad(x); 108 | y = g - gPrev; 109 | 110 | Hy = H * y; 111 | ys = dotProd(y, s); 112 | yHy = dotProd(y, Hy); 113 | if ((ys < EPS) || (yHy < EPS)) 114 | H = eye(N, Dtype(1.0)); 115 | else 116 | { 117 | v = sqrt(yHy) * (s / ys - Hy / yHy); 118 | H = H + multTr(s, s) / ys - multTr(Hy, Hy) / yHy + multTr(v, v); 119 | } 120 | gnorm[k++] = norm(g); 121 | } 122 | } 123 | 124 | xOpt = x; 125 | fMin = fx; 126 | gradNorm.resize(k); 127 | for (int i = 0; i tol) 131 | this->success = false; 132 | } 133 | 134 | 135 | /** 136 | * Get the optimum point. 137 | */ 138 | template 139 | inline Vector BFGS::getOptValue() const 140 | { 141 | return xOpt; 142 | } 143 | 144 | 145 | /** 146 | * Get the norm of gradient in each iteration. 147 | */ 148 | template 149 | inline Vector BFGS::getGradNorm() const 150 | { 151 | return gradNorm; 152 | } 153 | 154 | 155 | /** 156 | * Get the minimum value of objective function. 157 | */ 158 | template 159 | inline Dtype BFGS::getFuncMin() const 160 | { 161 | return fMin; 162 | } 163 | 164 | 165 | /** 166 | * Get the iteration number. 167 | */ 168 | template 169 | inline int BFGS::getItrNum() const 170 | { 171 | return gradNorm.dim() - 1; 172 | } -------------------------------------------------------------------------------- /MatrixFunction/bfgs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * bfgs.h 28 | * 29 | * BFGS quasi-Newton method. 30 | * 31 | * This class is designed for finding the minimum value of objective function 32 | * in one dimension or multidimension. Inexact line search algorithm is used 33 | * to get the step size in each iteration. BFGS (Broyden-Fletcher-Goldfarb 34 | * -Shanno) modifier formula is used to compute the inverse of Hesse matrix. 35 | * 36 | * Zhang Ming, 2010-03, Xi'an Jiaotong University. 37 | *****************************************************************************/ 38 | 39 | 40 | #ifndef BFGS_H 41 | #define BFGS_H 42 | 43 | 44 | #include "matrix.h" 45 | #include "linesearch.h" 46 | 47 | 48 | namespace splab 49 | { 50 | 51 | template 52 | class BFGS : public LineSearch 53 | { 54 | 55 | public: 56 | 57 | BFGS(); 58 | ~BFGS(); 59 | 60 | void optimize(Ftype &func, Vector &x0, Dtype tol = Dtype(1.0e-6), 61 | int maxItr = 100); 62 | 63 | Vector getOptValue() const; 64 | Vector getGradNorm() const; 65 | Dtype getFuncMin() const; 66 | int getItrNum() const; 67 | 68 | private: 69 | 70 | // iteration number 71 | int itrNum; 72 | 73 | // minimum value of objective function 74 | Dtype fMin; 75 | 76 | // optimal solution 77 | Vector xOpt; 78 | 79 | // gradient norm for each iteration 80 | Vector gradNorm; 81 | 82 | }; 83 | // class BFGS 84 | 85 | 86 | #include "bfgs-impl.h" 87 | 88 | } 89 | // namespace splab 90 | 91 | 92 | #endif 93 | // BFGS_H -------------------------------------------------------------------------------- /MatrixFunction/bfgs_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/bfgs_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/ccholesky-impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * ccholesky-impl.h 28 | * 29 | * Implementation for CCholesky (complex Cholesky) class. 30 | * 31 | * Zhang Ming, 2010-12, Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | /** 36 | * constructor and destructor 37 | */ 38 | template 39 | CCholesky::CCholesky() : spd(true) 40 | { 41 | } 42 | 43 | template 44 | CCholesky::~CCholesky() 45 | { 46 | } 47 | 48 | 49 | /** 50 | * return true, if original matrix is symmetric positive-definite. 51 | */ 52 | template 53 | inline bool CCholesky::isSpd() const 54 | { 55 | return spd; 56 | } 57 | 58 | 59 | /** 60 | * Constructs a lower triangular matrix L, such that L*L^H= A. 61 | * If A is not symmetric positive-definite (SPD), only a partial 62 | * factorization is performed. If isspd() evalutate true then 63 | * the factorizaiton was successful. 64 | */ 65 | template 66 | void CCholesky::dec(const Matrix &A) 67 | { 68 | int m = A.rows(); 69 | int n = A.cols(); 70 | 71 | spd = (m == n); 72 | if (!spd) 73 | return; 74 | 75 | L = Matrix(A.cols(), A.cols()); 76 | 77 | // main loop 78 | for (int j = 0; j 0); 96 | 97 | L[j][j] = sqrt(real(d) > 0 ? d : 0); 98 | for (int k = j + 1; k 108 | inline Matrix CCholesky::getL() const 109 | { 110 | return L; 111 | } 112 | 113 | 114 | /** 115 | * Solve a linear system A*x = b, using the previously computed 116 | * cholesky factorization of A: L*L'. 117 | */ 118 | template 119 | Vector CCholesky::solve(const Vector &b) 120 | { 121 | int n = L.rows(); 122 | if (b.dim() != n) 123 | return Vector(); 124 | 125 | Vector x = b; 126 | 127 | // solve L*y = b 128 | for (int k = 0; k= 0; --k) 138 | { 139 | for (int i = k + 1; i 154 | Matrix CCholesky::solve(const Matrix &B) 155 | { 156 | int n = L.rows(); 157 | if (B.rows() != n) 158 | return Matrix(); 159 | 160 | Matrix X = B; 161 | int nx = B.cols(); 162 | 163 | // solve L*Y = B 164 | for (int j = 0; j= 0; --k) 176 | { 177 | for (int i = k + 1; i 52 | class CCholesky 53 | { 54 | 55 | public: 56 | 57 | CCholesky(); 58 | ~CCholesky(); 59 | 60 | bool isSpd() const; 61 | void dec(const Matrix &A); 62 | Matrix getL() const; 63 | 64 | Vector solve(const Vector &b); 65 | Matrix solve(const Matrix &B); 66 | 67 | private: 68 | 69 | bool spd; 70 | Matrix L; 71 | 72 | }; 73 | // class CCholesky 74 | 75 | 76 | #include "ccholesky-impl.h" 77 | 78 | } 79 | // namespace splab 80 | 81 | 82 | #endif 83 | // CCHOLESKY_H -------------------------------------------------------------------------------- /MatrixFunction/ccholesky_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/ccholesky_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/cevd-impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * cevd-impl.h 28 | * 29 | * Implementation for CEVD class. 30 | * 31 | * Zhang Ming, 2010-12, Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | /** 36 | * constructor and destructor 37 | */ 38 | template 39 | CEVD::CEVD() : hermitian(true) 40 | { 41 | } 42 | 43 | template 44 | CEVD::~CEVD() 45 | { 46 | } 47 | 48 | 49 | /** 50 | * Check for symmetry, then construct the eigenvalue decomposition 51 | */ 52 | template 53 | void CEVD::dec(const Matrix > &A) 54 | { 55 | int N = A.cols(); 56 | 57 | assert(A.rows() == N); 58 | 59 | V = Matrix >(N, N); 60 | 61 | Matrix S(2 * N, 2 * N); 62 | for (int i = 0; i eig; 72 | eig.dec(S); 73 | 74 | if (eig.isSymmetric()) 75 | { 76 | hermitian = true; 77 | rd = Vector(N); 78 | 79 | Matrix RV = eig.getV(); 80 | Vector Rd = eig.getD(); 81 | 82 | for (int i = 0; i(RV[i][j2], RV[i + N][j2]); 90 | } 91 | } 92 | else 93 | { 94 | hermitian = false; 95 | d = Vector >(N); 96 | 97 | Matrix > cV = eig.getCV(); 98 | Vector > cd = eig.getCD(); 99 | 100 | for (int i = 0; i(cV[i][j2].real() - cV[i + N][j2].imag(), 108 | cV[i][j2].imag() + cV[i + N][j2].real()); 109 | } 110 | } 111 | } 112 | 113 | 114 | /** 115 | * If the matrix is Hermitian, then return true. 116 | */ 117 | template 118 | bool CEVD::isHertimian() const 119 | { 120 | return hermitian; 121 | } 122 | 123 | 124 | /** 125 | * Return the COMPLEX eigenvector matrix 126 | */ 127 | template 128 | inline Matrix > CEVD::getV() const 129 | { 130 | return V; 131 | } 132 | 133 | 134 | /** 135 | * Return the complex eigenvalues vector. 136 | */ 137 | template 138 | inline Vector > CEVD::getD() const 139 | { 140 | return d; 141 | } 142 | 143 | 144 | /** 145 | * Return the real eigenvalues vector. 146 | */ 147 | template 148 | inline Vector CEVD::getRD() const 149 | { 150 | return rd; 151 | } -------------------------------------------------------------------------------- /MatrixFunction/cevd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * cevd.h 28 | * 29 | * Class template of eigenvalues decomposition for complex matrix. 30 | * 31 | * For a complex matrix A, we have A*V = V*D, where the eigenvalue matrix D 32 | * is diagonal and the eigenvector matrix V is linear independence. That is, 33 | * the diagonal values of D are the eigenvalues and the columns of V represent 34 | * the corresponding eigenvectors of D. If A is Hermitian, then V is a unitary 35 | * matrix, which means A = V*D*V', and eigenvalues are all real numbers. 36 | * 37 | * The matrix V may be badly conditioned, or even singular, so the validity 38 | * of the equation A=V*D*inverse(V) depends upon the condition number of V. 39 | * 40 | * Zhang Ming, 2010-12, Xi'an Jiaotong University. 41 | *****************************************************************************/ 42 | 43 | 44 | #ifndef CEVD_H 45 | #define CEVD_H 46 | 47 | 48 | #include "evd.h" 49 | 50 | 51 | namespace splab 52 | { 53 | 54 | template 55 | class CEVD 56 | { 57 | 58 | public: 59 | 60 | CEVD(); 61 | ~CEVD(); 62 | 63 | // decomposition 64 | void dec(const Matrix > &A); 65 | 66 | // the eigenvalues are real or complex 67 | bool isHertimian() const; 68 | 69 | // get eigenvectors and 70 | Matrix > getV() const; 71 | Vector > getD() const; 72 | Vector getRD() const; 73 | 74 | private: 75 | 76 | bool hermitian; 77 | 78 | // eigenvectors and eigenvalues 79 | Matrix > V; 80 | Vector > d; 81 | Vector rd; 82 | 83 | }; 84 | // class CEVD 85 | 86 | 87 | #include "cevd-impl.h" 88 | 89 | } 90 | // namespace splab 91 | 92 | 93 | #endif 94 | // CEVD_H -------------------------------------------------------------------------------- /MatrixFunction/cevd_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/cevd_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/cholesky.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * cholesky.h 28 | * 29 | * Class template of Cholesky decomposition. 30 | * 31 | * For a symmetric, positive definite matrix A, this function computes the 32 | * Cholesky factorization, i.e. it computes a lower triangular matrix L such 33 | * that A = L*L'. If the matrix is not symmetric or positive definite, the 34 | * function computes only a partial decomposition. This can be tested with 35 | * the isSpd() flag. 36 | * 37 | * This class also supports factorization of complex matrix by specializing 38 | * some member functions. 39 | * 40 | * Adapted form Template Numerical Toolkit. 41 | * 42 | * Zhang Ming, 2010-01 (revised 2010-12), Xi'an Jiaotong University. 43 | *****************************************************************************/ 44 | 45 | 46 | #ifndef CHOLESKY_H 47 | #define CHOLESKY_H 48 | 49 | 50 | #include "matrix.h" 51 | 52 | 53 | namespace splab 54 | { 55 | 56 | template 57 | class Cholesky 58 | { 59 | 60 | public: 61 | 62 | Cholesky(); 63 | ~Cholesky(); 64 | 65 | bool isSpd() const; 66 | void dec(const Matrix &A); 67 | Matrix getL() const; 68 | 69 | Vector solve(const Vector &b); 70 | Matrix solve(const Matrix &B); 71 | 72 | private: 73 | 74 | bool spd; 75 | 76 | Matrix L; 77 | 78 | }; 79 | // class Cholesky 80 | 81 | 82 | #include "cholesky-impl.h" 83 | 84 | } 85 | // namespace splab 86 | 87 | 88 | #endif 89 | // CHOLESKY_H -------------------------------------------------------------------------------- /MatrixFunction/cholesky_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/cholesky_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/conjgrad-impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * conjgrad-impl.h 28 | * 29 | * Implementation for ConjGrad class. 30 | * 31 | * Zhang Ming, 2010-03, Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | /** 36 | * constructors and destructor 37 | */ 38 | template 39 | ConjGrad::ConjGrad() : LineSearch() 40 | { 41 | } 42 | 43 | template 44 | ConjGrad::~ConjGrad() 45 | { 46 | } 47 | 48 | 49 | /** 50 | * Finding the optimal solution. The default tolerance error and maximum 51 | * iteratin number are "tol=1.0e-6" and "maxItr=100", respectively. 52 | */ 53 | template 54 | void ConjGrad::optimize(Ftype &func, Vector &x0, 55 | int reLen, Dtype tol, int maxItr) 56 | { 57 | // initialize parameters. 58 | int k = 0, 59 | cnt = 0; 60 | Vector x(x0); 61 | Dtype fx = func(x); 62 | this->funcNum++; 63 | Vector gnorm(maxItr); 64 | Vector g = func.grad(x); 65 | gnorm[k++] = norm(g); 66 | 67 | Dtype alpha, 68 | beta; 69 | Vector d(x0.dim()), 70 | dPrev(x0.dim()), 71 | gPrev(x0.dim()); 72 | 73 | while ((gnorm(k) > tol) && (k < maxItr)) 74 | { 75 | // descent direction 76 | if (mod(k, reLen) == 1) 77 | d = -g; 78 | else 79 | { 80 | beta = dotProd(g, g - gPrev) / dotProd(gPrev, gPrev); 81 | d = beta*dPrev - g; 82 | } 83 | 84 | // one dimension searching 85 | alpha = this->getStep(func, x, d); 86 | 87 | if (!this->success) 88 | { 89 | dPrev = 0; 90 | cnt++; 91 | if (cnt == maxItr) 92 | break; 93 | } 94 | else 95 | { 96 | // update 97 | x += alpha*d; 98 | fx = func(x); 99 | this->funcNum++; 100 | dPrev = d; 101 | gPrev = g; 102 | g = func.grad(x); 103 | gnorm[k++] = norm(g); 104 | } 105 | } 106 | 107 | xOpt = x; 108 | fMin = fx; 109 | gradNorm.resize(k); 110 | for (int i = 0; i tol) 114 | this->success = false; 115 | } 116 | 117 | 118 | /** 119 | * Get the optimum point. 120 | */ 121 | template 122 | inline Vector ConjGrad::getOptValue() const 123 | { 124 | return xOpt; 125 | } 126 | 127 | 128 | /** 129 | * Get the norm of gradient in each iteration. 130 | */ 131 | template 132 | inline Vector ConjGrad::getGradNorm() const 133 | { 134 | return gradNorm; 135 | } 136 | 137 | 138 | /** 139 | * Get the minimum value of objective function. 140 | */ 141 | template 142 | inline Dtype ConjGrad::getFuncMin() const 143 | { 144 | return fMin; 145 | } 146 | 147 | 148 | /** 149 | * Get the iteration number. 150 | */ 151 | template 152 | inline int ConjGrad::getItrNum() const 153 | { 154 | return gradNorm.dim() - 1; 155 | } -------------------------------------------------------------------------------- /MatrixFunction/conjgrad.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * conjgrad.h 28 | * 29 | * Conjugate gradient optimal method. 30 | * 31 | * This class is designed for finding the minimum value of objective function 32 | * in one dimension or multidimension. Inexact line search and PRP (Polak- 33 | * Ribiere-Polyak) formula are used to get the step size and conjgate 34 | * coefficient "beta" in each iteration. 35 | * 36 | * Zhang Ming, 2010-03, Xi'an Jiaotong University. 37 | *****************************************************************************/ 38 | 39 | 40 | #ifndef CONJGRAD_H 41 | #define CONJGRAD_H 42 | 43 | 44 | #include "linesearch.h" 45 | #include "utilities.h" 46 | 47 | 48 | namespace splab 49 | { 50 | 51 | template 52 | class ConjGrad : public LineSearch 53 | { 54 | 55 | public: 56 | 57 | ConjGrad(); 58 | ~ConjGrad(); 59 | 60 | void optimize(Ftype &func, Vector &x0, int reLen, 61 | Dtype tol = Dtype(1.0e-6), int maxItr = 100); 62 | 63 | Vector getOptValue() const; 64 | Vector getGradNorm() const; 65 | Dtype getFuncMin() const; 66 | int getItrNum() const; 67 | 68 | private: 69 | 70 | // iteration number 71 | int itrNum; 72 | 73 | // minimum value of objective function 74 | Dtype fMin; 75 | 76 | // optimal solution 77 | Vector xOpt; 78 | 79 | // gradient norm for each iteration 80 | Vector gradNorm; 81 | 82 | }; 83 | // class ConjGrad 84 | 85 | 86 | #include "conjgrad-impl.h" 87 | 88 | } 89 | // namespace splab 90 | 91 | 92 | #endif 93 | // CONJGRAD_H -------------------------------------------------------------------------------- /MatrixFunction/conjgrad_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/conjgrad_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/constants.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * constants.h 28 | * 29 | * Some constants often used in numeric computing. 30 | * 31 | * Zhang Ming, 2010-01, Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | #ifndef CONSTANTS_H 36 | #define CONSTANTS_H 37 | 38 | 39 | namespace splab 40 | { 41 | 42 | const double EPS = 2.220446049250313e-016; 43 | 44 | const double PI = 3.141592653589793; 45 | const double TWOPI = 6.283185307179586; 46 | const double HALFPI = 1.570796326794897; 47 | const double D2R = 0.017453292519943; 48 | 49 | const double EXP = 2.718281828459046; 50 | 51 | const double RT2 = 1.414213562373095; 52 | const double IRT2 = 0.707106781186547; 53 | 54 | const int FORWARD = 1; 55 | const int INVERSE = 0; 56 | 57 | const int MAXTERM = 20; 58 | const int INITSIZE = 20; 59 | const int EXTFACTOR = 2; 60 | 61 | } 62 | // namespace splab 63 | 64 | 65 | #endif 66 | // CONSTANTS_H -------------------------------------------------------------------------------- /MatrixFunction/cqrd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * cqrd.h 28 | * 29 | * Class template of QR decomposition for complex matrix. 30 | * 31 | * For an m-by-n complex matrix A, the QR decomposition is an m-by-m unitary 32 | * matrix Q and an m-by-n upper triangular matrix R so that A = Q*R. 33 | * 34 | * For economy size, denotes p = min(m,n), then Q is m-by-p, and R is n-by-p, 35 | * this file provides the economic decomposition format. 36 | * 37 | * The QR decompostion always exists, even if the matrix does not have full 38 | * rank, so the constructor will never fail. The Q and R factors can be 39 | * retrived via the getQ() and getR() methods. Furthermore, a solve() method 40 | * is provided to find the least squares solution of Ax=b or AX=B using the 41 | * QR factors. 42 | * 43 | * Zhang Ming, 2010-12, Xi'an Jiaotong University. 44 | *****************************************************************************/ 45 | 46 | 47 | #ifndef CQRD_H 48 | #define CQRD_H 49 | 50 | 51 | #include "matrix.h" 52 | 53 | 54 | namespace splab 55 | { 56 | 57 | template 58 | class CQRD 59 | { 60 | 61 | public: 62 | 63 | CQRD(); 64 | ~CQRD(); 65 | 66 | void dec(const Matrix > &A); 67 | bool isFullRank() const; 68 | 69 | Matrix > getQ(); 70 | Matrix > getR(); 71 | 72 | Vector > solve(const Vector > &b); 73 | Matrix > solve(const Matrix > &B); 74 | 75 | private: 76 | 77 | // internal storage of QR 78 | Matrix > QR; 79 | 80 | // diagonal of R 81 | Vector > diagR; 82 | 83 | // constants for generating Householder vector 84 | Vector betaR; 85 | 86 | 87 | }; 88 | // class CQRD 89 | 90 | 91 | #include "cqrd-impl.h" 92 | 93 | } 94 | // namespace splab 95 | 96 | 97 | #endif 98 | // CQRD_H -------------------------------------------------------------------------------- /MatrixFunction/cqrd_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/cqrd_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/csvd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * csvd.h 28 | * 29 | * Class template for complex matrix Singular Value Decomposition. 30 | * 31 | * For an m-by-n complex matrix A, the singular value decomposition is an 32 | * m-by-m unitary matrix U, an m-by-n diagonal matrix S, and an n-by-n 33 | * unitary matrix V so that A = U*S*V^H. 34 | * 35 | * The singular values, sigma[k] = S[k][k], are ordered so that sigma[0] >= 36 | * sigma[1] >= ... >= sigma[n-1]. 37 | * 38 | * For economy size, denotes p = min(m,n), then U is m-by-p, S is p-by-p, 39 | * and V is n-by-p, this file provides the economic decomposition format. 40 | * 41 | * The singular value decompostion always exists, so the constructor will 42 | * never fail. The matrix condition number and the effective numerical rank 43 | * can be computed from this decomposition. 44 | * 45 | * This code is adapted from Collected Algorithms from ACM, Algorithm 358, 46 | * Peter Businger, Gene Golub. 47 | * 48 | * Zhang Ming, 2010-12, Xi'an Jiaotong University. 49 | *****************************************************************************/ 50 | 51 | 52 | #ifndef CSVD_H 53 | #define CSVD_H 54 | 55 | 56 | #include "matrix.h" 57 | 58 | 59 | namespace splab 60 | { 61 | 62 | template 63 | class CSVD 64 | { 65 | 66 | public: 67 | 68 | CSVD(); 69 | ~CSVD(); 70 | 71 | void dec(const Matrix< complex >&); 72 | Matrix< complex > getU() const; 73 | Matrix< complex > getV() const; 74 | Matrix getSM(); 75 | Vector getSV() const; 76 | 77 | Type norm2() const; 78 | Type cond() const; 79 | int rank(); 80 | 81 | private: 82 | 83 | // the orthogonal matrix and singular value vector 84 | Matrix< complex > U; 85 | Matrix< complex > V; 86 | Vector S; 87 | 88 | // docomposition for matrix with rows >= columns 89 | void decomposition(Matrix< complex >&, 90 | Matrix< complex >&, 91 | Vector&, 92 | Matrix< complex >&); 93 | 94 | }; 95 | // class CSVD 96 | 97 | 98 | #include "csvd-impl.h" 99 | 100 | } 101 | // namespace splab 102 | 103 | 104 | #endif 105 | // CSVD_H -------------------------------------------------------------------------------- /MatrixFunction/csvd_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/csvd_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/evd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * evd.h 28 | * 29 | * Class template of eigenvalues and eigenvectors decomposition. 30 | * 31 | * For a real matrix A, we have A*V = V*D, where the eigenvalue matrix D is 32 | * diagonal and the eigenvector matrix V is linear independence. That is the 33 | * kth diagonal value of D is the eigenvalue and the kth column of V 34 | * represents the corresponding eigenvector of D[k][k]. If A is symmetric, 35 | * then V is a orthogonal matrix, which means A = V*D*V', and eigenvalues 36 | * are all real numbers. 37 | * 38 | * If A is not symmetric, then the eigenvalue matrix D is block diagonal 39 | * with the real eigenvalues in 1-by-1 blocks and any complex eigenvalues, 40 | * a+i*b, in 2-by-2 blocks [a,b; -b,a]. That is, if the complex eigenvalues 41 | * look like 42 | * 43 | * u + iv . . . . . 44 | * . u - iv . . . . 45 | * . . a + ib . . . 46 | * . . . a - ib . . 47 | * . . . . x . 48 | * . . . . . y 49 | * 50 | * then D looks like 51 | * 52 | * u v . . . . 53 | * -v u . . . . 54 | * . . a b . . 55 | * . . -b a . . 56 | * . . . . x . 57 | * . . . . . y 58 | * 59 | * This keeps V a real matrix in both symmetric and non-symmetric cases, and 60 | * A*V = V*D. 61 | * 62 | * The matrix V may be badly conditioned, or even singular, so the validity 63 | * of the equation A=V*D*inverse(V) depends upon the condition number of V. 64 | * 65 | * Adapted form Template Numerical Toolkit. 66 | * 67 | * Zhang Ming, 2010-01 (revised 2010-12), Xi'an Jiaotong University. 68 | *****************************************************************************/ 69 | 70 | 71 | #ifndef EVD_H 72 | #define EVD_H 73 | 74 | 75 | #include "matrix.h" 76 | 77 | 78 | namespace splab 79 | { 80 | 81 | template 82 | class EVD 83 | { 84 | 85 | public: 86 | 87 | EVD(); 88 | ~EVD(); 89 | 90 | // decomposition 91 | void dec(const Matrix &A); 92 | 93 | // the eigenvalues are real or complex 94 | bool isSymmetric() const; 95 | bool isComplex(Real tol = Real(EPS)); 96 | 97 | // get eigenvectors 98 | Matrix getV() const; 99 | Matrix > getCV(); 100 | 101 | // get eigenvalues 102 | Vector getD() const; 103 | Vector > getCD(); 104 | // Matrix getDM(); 105 | // Matrix > getCDM(); 106 | 107 | private: 108 | 109 | int n; 110 | bool symmetric; 111 | Real cdivr, 112 | cdivi; 113 | 114 | // eigenvalues and its real and image part 115 | Matrix V; 116 | Vector d; 117 | Vector e; 118 | 119 | // temporary storage for internal variables 120 | Vector ort; 121 | Matrix H; 122 | 123 | void tred2(); 124 | void tql2(); 125 | void cdiv(Real xr, Real xi, Real yr, Real yi); 126 | void hqr2(); 127 | void others(); 128 | void normalized(); 129 | 130 | }; 131 | // class EVD 132 | 133 | 134 | #include "evd-impl.h" 135 | 136 | } 137 | // namespace splab 138 | 139 | 140 | #endif 141 | // EVD_H -------------------------------------------------------------------------------- /MatrixFunction/evd_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/evd_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/fft-impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * fft-impl.h 28 | * 29 | * Implementation for FFT and IFFT interface. 30 | * 31 | * Zhang Ming, 2010-09, Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | /** 36 | * Forward FFT algorithm. 37 | */ 38 | template 39 | inline Vector< complex > fft(const Vector &xn) 40 | { 41 | return fftr2c(xn); 42 | } 43 | 44 | template 45 | inline Vector< complex > fft(const Vector< complex > &xn) 46 | { 47 | return fftc2c(xn); 48 | } 49 | 50 | 51 | /** 52 | * Inverse FFT algorithm. 53 | */ 54 | template 55 | inline Vector< complex > ifft(const Vector< complex > &Xk) 56 | { 57 | return ifftc2c(Xk); 58 | } 59 | 60 | 61 | /** 62 | * Real to complex DFT of 1D signal. 63 | */ 64 | template 65 | inline Vector< complex > fftr2c(const Vector &xn) 66 | { 67 | Vector< complex > Xk(xn.size()); 68 | 69 | if (isPower2(xn.size())) 70 | { 71 | FFTMR dft; 72 | dft.fft(xn, Xk); 73 | } 74 | else 75 | { 76 | FFTPF dft; 77 | dft.fft(xn, Xk); 78 | } 79 | 80 | return Xk; 81 | } 82 | 83 | 84 | /** 85 | * Complex to complex DFT of 1D signal. 86 | */ 87 | template 88 | inline Vector< complex > fftc2c(const Vector< complex > &xn) 89 | { 90 | Vector< complex > Xk(xn.size()); 91 | 92 | if (isPower2(xn.size())) 93 | { 94 | for (int i = 0; i dft; 97 | dft.fft(Xk); 98 | } 99 | else 100 | { 101 | FFTPF dft; 102 | dft.fft(xn, Xk); 103 | } 104 | 105 | return Xk; 106 | } 107 | 108 | 109 | /** 110 | * Complex to real IDFT of 1D signal. 111 | */ 112 | template 113 | inline Vector ifftc2r(const Vector< complex > &Xk) 114 | { 115 | Vector xn(Xk.size()); 116 | 117 | if (isPower2(xn.size())) 118 | { 119 | Vector< complex > tmp(Xk); 120 | FFTMR dft; 121 | dft.ifft(tmp, xn); 122 | } 123 | else 124 | { 125 | FFTPF dft; 126 | dft.ifft(Xk, xn); 127 | } 128 | 129 | return xn; 130 | } 131 | 132 | 133 | /** 134 | * Complex to complex IDFT of 1D signal. 135 | */ 136 | template 137 | inline Vector< complex > ifftc2c(const Vector< complex > &Xk) 138 | { 139 | Vector< complex > xn(Xk.size()); 140 | 141 | if (isPower2(xn.size())) 142 | { 143 | for (int i = 0; i dft; 146 | dft.ifft(xn); 147 | } 148 | else 149 | { 150 | FFTPF dft; 151 | dft.ifft(Xk, xn); 152 | } 153 | 154 | return xn; 155 | } -------------------------------------------------------------------------------- /MatrixFunction/fft.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * fft.h 28 | * 29 | * Some convenient interface for FFT algorithm. If signal length "N" is power 30 | * of 2, then calls FFTR2 class to compute, else calls FFTPF class. 31 | * Forward: Xk = fftr2c(xn); Xk = fftc2c(xn); 32 | * Inverse: xn = fftc2r(Xk); xn = fftc2c(Xk); 33 | * 34 | * These routines don't need FFTW lib, but less efficiency than FFTW. 35 | * 36 | * Zhang Ming, 2010-09, Xi'an Jiaotong University. 37 | *****************************************************************************/ 38 | 39 | 40 | #ifndef FFT_H 41 | #define FFT_H 42 | 43 | 44 | #include "fftmr.h" 45 | #include "fftpf.h" 46 | 47 | 48 | namespace splab 49 | { 50 | 51 | template 52 | Vector< complex > fft(const Vector&); 53 | template 54 | Vector< complex > fft(const Vector< complex >&); 55 | template 56 | Vector< complex > ifft(const Vector< complex >&); 57 | 58 | template 59 | Vector< complex > fftr2c(const Vector&); 60 | template 61 | Vector< complex > fftc2c(const Vector< complex >&); 62 | 63 | template 64 | Vector ifftc2r(const Vector< complex >&); 65 | template 66 | Vector< complex > ifftc2c(const Vector< complex >&); 67 | 68 | 69 | #include "fft-impl.h" 70 | 71 | } 72 | // namespace splab 73 | 74 | 75 | #endif 76 | // FFT_H -------------------------------------------------------------------------------- /MatrixFunction/fft_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/fft_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/fftmr.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * fftmr.h 28 | * 29 | * Fast Fourier Transform with Mixed Radix Algorithm 30 | * 31 | * This class is designed for calculating discrete Fourier transform and 32 | * inverse discrete Fourier transform of 1D signals by using Mixed-Radix 33 | * Algorithms. The length of signals should equal to powers of 2. 34 | * 35 | * The algorithm is modified from "kube-gustavson-fft.c". Which is a pretty 36 | * fast FFT algorithm. Not super-optimized lightning-fast, but very good 37 | * considering its age and relative simplicity. 38 | * 39 | * Zhang Ming, 2010-04, Xi'an Jiaotong University. 40 | *****************************************************************************/ 41 | 42 | 43 | #ifndef FFTMR_H 44 | #define FFTMR_H 45 | 46 | 47 | #include "vector.h" 48 | 49 | 50 | namespace splab 51 | { 52 | 53 | /** 54 | * complex node converted from "C" version for this algorithm 55 | */ 56 | template 57 | struct Complex 58 | { 59 | Type re; 60 | Type im; 61 | }; 62 | 63 | 64 | /** 65 | * two routines frequently used in FFT algorithm 66 | */ 67 | bool isPower2(int); 68 | int fastLog2(int); 69 | 70 | 71 | /** 72 | * Radix based FFT class 73 | */ 74 | template 75 | class FFTMR 76 | { 77 | 78 | public: 79 | 80 | FFTMR(); 81 | ~FFTMR(); 82 | 83 | void fft(Vector< complex > &xn); 84 | void ifft(Vector< complex > &Xk); 85 | void fft(const Vector &xn, Vector< complex > &Xk); 86 | void ifft(const Vector< complex > &Xk, Vector &xn); 87 | 88 | private: 89 | 90 | void bitReverse(Vector &bitrev); 91 | void radix2(int nthpo, Complex *c0, Complex *c1); 92 | void radix4(int nthpo, Complex *c0, Complex *c1, 93 | Complex *c2, Complex *c3); 94 | void radix8(int nxtlt, int nthpo, int length, 95 | Complex *cc0, Complex *cc1, 96 | Complex *cc2, Complex *cc3, 97 | Complex *cc4, Complex *cc5, 98 | Complex *cc6, Complex *cc7); 99 | void dft(int direction, int n, Complex *b); 100 | }; 101 | // class FFTMR 102 | 103 | 104 | #include "fftmr-impl.h" 105 | 106 | } 107 | // namespace splab 108 | 109 | 110 | #endif 111 | // FFTMR_H -------------------------------------------------------------------------------- /MatrixFunction/fftmr_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/fftmr_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/fftpf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * fftpf.h 28 | * 29 | * Fast Fourier Transform with prime factor algorithm 30 | * 31 | * This class is designed for calculating discrete Fourier transform and 32 | * inverse discrete Fourier transform of 1D signals by using prime factor 33 | * algorithms. The signals can be arbitrary length. The largest prime factor 34 | * of n must be less than or equal to the constant PRIMEFACTOR defined below. 35 | * 36 | * The general idea is to factor the length of the DFT "n" into factors that are 37 | * efficiently handled by the routines. A number of short DFT's are implemented 38 | * with a minimum of arithmetical operations and using(almost) straight line 39 | * code resultingin very fast execution when the factors of n belong to this 40 | * set. Especially radix-10 is optimized. Prime factors, that are not in the 41 | * set of short DFT's are handled with direct evaluation of the DFP expression. 42 | * 43 | * The algorithm is modified from "xFFT.h" of "Pratical Fourier Transform 44 | * and C++ Implementation" written by Hequan Sun. 45 | * 46 | * Zhang Ming, 2010-09, Xi'an Jiaotong University. 47 | *****************************************************************************/ 48 | 49 | 50 | #ifndef FFTPF_H 51 | #define FFTPF_H 52 | 53 | 54 | #include "vector.h" 55 | 56 | #define PRIMEFACTOR 37 57 | #define PRIMEFACTORHALF (PRIMEFACTOR+1)/2 58 | #define PRIMECOUNT 20 59 | 60 | 61 | namespace splab 62 | { 63 | 64 | template 65 | class FFTPF 66 | { 67 | 68 | public: 69 | 70 | FFTPF(); 71 | ~FFTPF(); 72 | 73 | void fft(const Vector &xn, Vector< complex > &Xk); 74 | void ifft(const Vector< complex > &Xk, Vector &xn); 75 | 76 | void fft(const Vector< complex > &xn, 77 | Vector< complex > &Xk); 78 | void ifft(const Vector< complex > &Xk, 79 | Vector< complex > &xn); 80 | 81 | private: 82 | 83 | bool bAlloc; 84 | int mNOldSize, mNFactor, nNewFactorSize, nHalfFactorSize, 85 | groupOffset, dataOffset, blockOffset, adr, groupNo, 86 | dataNo, blockNo, twNo; 87 | int mSofarRadix[PRIMECOUNT], mActualRadix[PRIMECOUNT], 88 | mRemainRadix[PRIMECOUNT]; 89 | 90 | Type tPI, t2PI, tIRT2, tIRT3, omega, twRe, twIim; 91 | Type *pftwRe, *pftgRe, *pfzRe, *pfvRe, *pfwRe, *pftwIm, *pftgIm, 92 | *pfzIm, *pfvIm, *pfwIm; 93 | Type twiddleRe[PRIMEFACTOR], trigRe[PRIMEFACTOR], zRe[PRIMEFACTOR], 94 | twiddleIm[PRIMEFACTOR], trigIm[PRIMEFACTOR], zIm[PRIMEFACTOR], 95 | vRe[PRIMEFACTORHALF], wRe[PRIMEFACTORHALF], 96 | vIm[PRIMEFACTORHALF], wIm[PRIMEFACTORHALF]; 97 | 98 | Type c3_1, 99 | c5_1, c5_2, c5_3, c5_4, c5_5, 100 | c7_1, c7_2, c7_3, c7_4, c7_5, c7_6, 101 | c9_2, c9_3, c9_4, c9_5, c9_6, c9_7, c9_8, c9_9, 102 | c11_1, c11_2, c11_3, c11_4, c11_5, c11_6, c11_7, c11_8, 103 | c11_9, c11_10, c13_1, c13_2, c13_3, c13_4, c13_5, c13_6, 104 | c13_7, c13_8, c13_9, c13_10, c13_11, c13_12, c16_2, c16_3, 105 | c16_4, c16_5; 106 | 107 | Type ttmp, 108 | t1_re, t1_im, t2_re, t2_im, t3_re, t3_im, t4_re, t4_im, 109 | t5_re, t5_im, t6_re, t6_im, t7_re, t7_im, t8_re, t8_im, 110 | t9_re, t9_im, t10_re, t10_im, t11_re, t11_im, t12_re, t12_im, 111 | t13_re, t13_im, t14_re, t14_im, t15_re, t15_im, t16_re, t16_im, 112 | t17_re, t17_im, t18_re, t18_im, t19_re, t19_im, t20_re, t20_im, 113 | t21_re, t21_im, t22_re, t22_im, 114 | m1_re, m1_im, m2_re, m2_im, m3_re, m3_im, m4_re, m4_im, 115 | m5_re, m5_im, m6_re, m6_im, m7_re, m7_im, m8_re, m8_im, 116 | m9_re, m9_im, m10_re, m10_im, m11_re, m11_im, m12_re, m12_im; 117 | 118 | void releaseMem(); 119 | void allocateMem(); 120 | void factorize(int n, int &nFact, int *fact); 121 | void primeSetup(int nPoints); 122 | void permute(const Vector &xn, Vector< complex > &yn); 123 | void permute(const Vector< complex > &xn, Vector< complex > &yn, 124 | bool bTrans = true); 125 | void initTrig(int radix); 126 | void radix2(Type *aRe, Type *aIm); 127 | void radix3(Type *aRe, Type *aIm); 128 | void radix4(Type *aRe, Type *aIm); 129 | void radix5(Type *aRe, Type *aIm); 130 | void radix7(Type *aRe, Type *aIm); 131 | void radix8(Type *aRe, Type *aIm); 132 | void radix9(Type *aRe, Type *aIm); 133 | void radix10(Type *aRe, Type *aIm); 134 | void radix11(Type *aRe, Type *aIm); 135 | void radix13(Type *aRe, Type *aIm); 136 | void radix16(Type *aRe, Type *aIm); 137 | void radixOther(int radix); 138 | void twiddleFFT(int sofarRadix, int radix, int remainRadix, 139 | Vector< complex > &yn); 140 | 141 | }; 142 | // class FFTPF 143 | 144 | 145 | #include "fftpf-impl.h" 146 | 147 | } 148 | // namespace splab 149 | 150 | 151 | #endif 152 | //FFTPF_H -------------------------------------------------------------------------------- /MatrixFunction/fftpf_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/fftpf_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/fitcurves.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * fitcurves.h 28 | * 29 | * Fitted functions for least square fitting. 30 | * 31 | * Zhang Ming, 2010-04, Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | #ifndef FITCURVES_H 36 | #define FITCURVES_H 37 | 38 | 39 | #include 40 | #include "constants.h" 41 | 42 | 43 | namespace splab 44 | { 45 | 46 | template 47 | class Funcs 48 | { 49 | 50 | public: 51 | 52 | const static int dim = 4; 53 | 54 | // Compute the value of functions at point x. 55 | Type operator()(int i, const Type &x) 56 | { 57 | switch (i) 58 | { 59 | case 1: 60 | { 61 | return 1; 62 | break; 63 | } 64 | case 2: 65 | { 66 | return log(max(x, EPS)); 67 | break; 68 | } 69 | case 3: 70 | { 71 | return x; 72 | break; 73 | } 74 | case 4: 75 | { 76 | return x*x; 77 | break; 78 | } 79 | default: 80 | { 81 | std::cerr << "The dimension 'i' exceed the bound!" 82 | << std::endl; 83 | return 0; 84 | break; 85 | } 86 | } 87 | } 88 | 89 | }; 90 | // class ObjFunc 91 | 92 | } 93 | // namespace splab 94 | 95 | 96 | #endif 97 | // FITCURVES_H -------------------------------------------------------------------------------- /MatrixFunction/integral-impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * integral-impl.h 28 | * 29 | * Implementation for Romberg method. 30 | * 31 | * Zhang Ming, 2010-04, Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | template 36 | Type romberg(Func &f, const Type &a, const Type &b, const Type tol) 37 | { 38 | int n = 5, 39 | m = 2 * n; 40 | Type x = 0, 41 | tmp = 0, 42 | result = 0; 43 | 44 | for (int k = 0; k 40 | #include "integrand.h" 41 | 42 | 43 | namespace splab 44 | { 45 | 46 | template Type romberg(Func&, 47 | const Type&, const Type&, 48 | const Type tol = Type(1.0e-6)); 49 | 50 | 51 | #include "integral-impl.h" 52 | 53 | } 54 | // namespace splab 55 | 56 | 57 | #endif 58 | // INTEGRAL_H -------------------------------------------------------------------------------- /MatrixFunction/integral_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/integral_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/integrand.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * integrand.h 28 | * 29 | * Integrand used in numerical integral. 30 | * 31 | * Zhang Ming, 2010-04, Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | #ifndef INTEGRAND_H 36 | #define INTEGRAND_H 37 | 38 | 39 | #include 40 | #include "constants.h" 41 | 42 | 43 | namespace splab 44 | { 45 | 46 | /** 47 | * Integrand, a function objective. 48 | */ 49 | template 50 | class Func 51 | { 52 | 53 | public: 54 | 55 | /** 56 | * Initialize the parameters 57 | */ 58 | Func(Type aa, Type bb) : a(aa), b(bb) 59 | { } 60 | 61 | /** 62 | * Compute the value of objective function at point x. 63 | */ 64 | Type operator()(Type x) 65 | { 66 | return a + b*sin(x); 67 | } 68 | 69 | private: 70 | 71 | Type a, 72 | b; 73 | 74 | }; 75 | // class Func 76 | 77 | } 78 | // namespace splab 79 | 80 | 81 | #endif 82 | // INTEGRAND_H -------------------------------------------------------------------------------- /MatrixFunction/interpolation.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * interpolation.h 28 | * 29 | * Base class for interpolation. 30 | * 31 | * Zhang Ming, 2010-04, Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | #ifndef INTERPOLATION_H 36 | #define INTERPOLATION_H 37 | 38 | 39 | #include "vector.h" 40 | 41 | 42 | namespace splab 43 | { 44 | 45 | template 46 | class Interpolation 47 | { 48 | 49 | public: 50 | 51 | Interpolation(const Vector &xn, const Vector &yn) 52 | : xi(xn), yi(yn) 53 | { 54 | } 55 | 56 | virtual ~Interpolation() 57 | { 58 | } 59 | 60 | virtual void calcCoefs() = 0; 61 | virtual Type evaluate(Type x) = 0; 62 | 63 | protected: 64 | 65 | Vector xi; // abscissas 66 | Vector yi; // ordinates 67 | 68 | }; 69 | // class Interpolation 70 | 71 | } 72 | // namespace splab 73 | 74 | 75 | #endif 76 | // INTERPOLATION_H -------------------------------------------------------------------------------- /MatrixFunction/inverse-impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * inverse-impl.h 28 | * 29 | * Implementation for matrix inverse 30 | * 31 | * Zhang Ming, 2010-08 (revised 2010-12), Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | /** 36 | * Cpmpute the inverse of square real matrix. 37 | */ 38 | template 39 | Matrix inv(const Matrix &A, const string &type) 40 | { 41 | int n = A.rows(); 42 | assert(n == A.cols()); 43 | 44 | if (type == "spd") 45 | { 46 | Cholesky cho; 47 | cho.dec(A); 48 | if (cho.isSpd()) 49 | return cho.solve(eye(n, Type(1))); 50 | else 51 | { 52 | cerr << "The matrix is not symmetric!" << endl; 53 | return A; 54 | } 55 | } 56 | else 57 | { 58 | LUD lu; 59 | lu.dec(A); 60 | return lu.solve(eye(n, Type(1))); 61 | } 62 | } 63 | 64 | 65 | /** 66 | * Gauss-jordan column pivot elimination for computing matrix's inverse. 67 | * The matrix can be both REAL or COMPLEX. 68 | */ 69 | template 70 | Matrix colPivInv(const Matrix &A) 71 | { 72 | int rows = A.rows(); 73 | int clumns = A.cols(); 74 | 75 | assert(rows == clumns); 76 | 77 | Matrix invA(A); 78 | Vector index(rows); 79 | int i, j, k; 80 | Type tmp = 0; 81 | 82 | for (k = 0; k abs(mvl)) 91 | { 92 | mvl = tmp; 93 | index[k] = i; 94 | } 95 | } 96 | if (abs(mvl) < EPS) 97 | { 98 | cerr << "\n" << "A is a singular matrix." << "\n"; 99 | return Matrix(0, 0); 100 | } 101 | 102 | if (index[k] != k) 103 | { 104 | tmp = 0; 105 | for (j = 0; j= 0; --k) 134 | if (index[k] != k) 135 | { 136 | tmp = 0; 137 | for (i = 0; i 154 | Matrix cmpPivInv(const Matrix &A) 155 | { 156 | int n = A.rows(); 157 | assert(n == A.cols()); 158 | Matrix invA(A); 159 | 160 | int k; 161 | Type tmp = 0, 162 | mvl = 0; 163 | Vector rowIndex(n), 164 | colIndex(n); 165 | 166 | for (k = 0; k abs(mvl)) 175 | { 176 | mvl = tmp; 177 | rowIndex[k] = i; 178 | colIndex[k] = j; 179 | } 180 | } 181 | 182 | if (abs(mvl) < EPS) 183 | { 184 | cerr << endl << "A is a singular matrix." << endl; 185 | return invA; 186 | } 187 | 188 | // row exchange 189 | if (rowIndex[k] != k) 190 | for (int i = 0; i= 0; --k) 218 | { 219 | if (colIndex[k] != k) 220 | for (int j = 0; j 237 | Matrix > cinv(const Matrix > &M, 238 | const string &type) 239 | { 240 | int n = M.rows(); 241 | assert(n == M.cols()); 242 | 243 | Matrix A = real(M), 244 | B = imag(M), 245 | C = inv(A + B*inv(A, type)*B), 246 | D = -inv(B + A*inv(B, type)*A); 247 | 248 | Matrix > IM(n, n); 249 | for (int i = 0; i(C[i][j], D[i][j]); 252 | 253 | return IM; 254 | } -------------------------------------------------------------------------------- /MatrixFunction/inverse.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * inverse.h 28 | * 29 | * Matrix Inverse 30 | * 31 | * Matrix's inverse is a very important algorithm. If 'A' is an n-by-n full 32 | * rank square matrix, then it's inverse 'invA' is also an n-by-n full rank 33 | * square matrix. The general method to compute such matrix's inverse is 34 | * use LU decomposition to solve A*invA=I, if 'A' is SPD, then Cholesky 35 | * decomposition will be more efficiency. 36 | * 37 | * The algorithms provided in this file can be applied both for REAL matrix 38 | * or COMPLEX matrix. 39 | * 40 | * Zhang Ming, 2010-08 (revised 2010-12), Xi'an Jiaotong University. 41 | *****************************************************************************/ 42 | 43 | 44 | #ifndef INVERSE_H 45 | #define INVERSE_H 46 | 47 | 48 | #include 49 | #include "matrix.h" 50 | #include "cholesky.h" 51 | #include "lud.h" 52 | 53 | 54 | namespace splab 55 | { 56 | 57 | template Matrix inv(const Matrix&, 58 | const string &type = "nspd"); 59 | template 60 | Matrix > cinv(const Matrix >&, 61 | const string &type = "nspd"); 62 | 63 | template Matrix colPivInv(const Matrix&); 64 | template Matrix cmpPivInv(const Matrix&); 65 | 66 | 67 | #include "inverse-impl.h" 68 | } 69 | // namespace splab 70 | 71 | 72 | #endif 73 | // INVERSE_H -------------------------------------------------------------------------------- /MatrixFunction/inverse_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/inverse_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/linequs1.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * linequs1.h 28 | * 29 | * Function template for solving deterministic linear equations. 30 | * 31 | * For a n-by-n coefficient matrix A and n-by-1 constant vector b, Gauss 32 | * elimination method can solve the equations Ax=b. But the decomposition 33 | * methods are more commonly used. if A is not SPD, the LU decomposition 34 | * can be use to solve the equations; if A is SPD, the Cholesky decomposition 35 | * is the better choice; if A is a tridiagonal matrix, then the Forward 36 | * Elimination and Backward Substitution maybe the best choice. 37 | * 38 | * These functions can be used by both REAL or COMPLEX linear equations. 39 | * 40 | * Zhang Ming, 2010-07 (revised 2010-12), Xi'an Jiaotong University. 41 | *****************************************************************************/ 42 | 43 | 44 | #ifndef DETLINEQUS_H 45 | #define DETLINEQUS_H 46 | 47 | 48 | #include "vector.h" 49 | #include "matrix.h" 50 | #include "cholesky.h" 51 | #include "lud.h" 52 | 53 | 54 | namespace splab 55 | { 56 | template 57 | Vector gaussSolver(const Matrix&, const Vector&); 58 | template 59 | void gaussSolver(Matrix&, Matrix&); 60 | 61 | template 62 | Vector luSolver(const Matrix&, const Vector&); 63 | template 64 | Matrix luSolver(const Matrix&, const Matrix&); 65 | 66 | template 67 | Vector choleskySolver(const Matrix&, const Vector&); 68 | template 69 | Matrix choleskySolver(const Matrix&, const Matrix&); 70 | 71 | template 72 | Vector utSolver(const Matrix&, const Vector&); 73 | template 74 | Vector ltSolver(const Matrix&, const Vector&); 75 | 76 | template 77 | Vector febsSolver(const Vector&, const Vector&, 78 | const Vector&, const Vector&); 79 | 80 | 81 | #include "linequs1-impl.h" 82 | 83 | } 84 | // namespace splab 85 | 86 | 87 | #endif 88 | // DETLINEQUS_H -------------------------------------------------------------------------------- /MatrixFunction/linequs1_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/linequs1_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/linequs2.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * linequs2.h 28 | * 29 | * Function template for solving linear equations. 30 | * 31 | * For a m-by-n (m!=n) coefficient matrix A and m-by-1 constant vector b, if 32 | * m>n, the exact solution of Ax=b is not existent, but we can find the least 33 | * square solution that minimize norm of Ax-b; if m 59 | Vector lsSolver(const Matrix&, const Vector&); 60 | template 61 | Vector qrLsSolver(const Matrix&, const Vector&); 62 | template 63 | Vector svdLsSolver(const Matrix&, const Vector&); 64 | template 65 | Vector > qrLsSolver(const Matrix >&, 66 | const Vector >&); 67 | template 68 | Vector > svdLsSolver(const Matrix >&, 69 | const Vector >&); 70 | 71 | template 72 | Vector lnSolver(const Matrix&, const Vector&); 73 | template 74 | Vector qrLnSolver(const Matrix&, const Vector&); 75 | template 76 | Vector svdLnSolver(const Matrix&, const Vector&); 77 | template 78 | Vector > qrLnSolver(const Matrix >&, 79 | const Vector >&); 80 | template 81 | Vector > svdLnSolver(const Matrix >&, 82 | const Vector >&); 83 | 84 | 85 | #include "linequs2-impl.h" 86 | 87 | } 88 | // namespace splab 89 | 90 | 91 | #endif 92 | // UNDETLINEQUS_H -------------------------------------------------------------------------------- /MatrixFunction/linequs2_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/linequs2_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/linequs3.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * linequs3.h 28 | * 29 | * Function template for solving Rank Defect linear equations. 30 | * 31 | * For a m-by-n coefficient matrix A and m-by-1 constant vector b, if A is a 32 | * Rank Deficient Matrix, then the conventional methods will fail to solve 33 | * Ax = b. Three methods for solving such problem are provided in this file, 34 | * they are: Truncated SVD, Dampted SVD and Tikhonov Regularization. 35 | * 36 | * These methods adapt to both REAL or COMPLEX linear equations. 37 | * 38 | * Zhang Ming, 2010-07 (revised 2010-12), Xi'an Jiaotong University. 39 | *****************************************************************************/ 40 | 41 | 42 | #ifndef RANKDEFLINEQUS_H 43 | #define RANKDEFLINEQUS_H 44 | 45 | 46 | #include "svd.h" 47 | #include "csvd.h" 48 | 49 | 50 | namespace splab 51 | { 52 | 53 | template 54 | Vector tsvd(const Matrix&, const Vector&, 55 | Real tol = Real(-1.0)); 56 | template 57 | Vector > tsvd2(const Matrix >&, 58 | const Vector >&, Type tol = Type(-1.0)); 59 | 60 | template 61 | Vector dsvd(const Matrix&, const Vector&, Real&); 62 | template 63 | Vector > dsvd(const Matrix >&, 64 | const Vector >&, Type&); 65 | 66 | template 67 | Vector tikhonov(const Matrix&, const Vector&, Real&); 68 | template 69 | Vector > tikhonov(const Matrix >&, 70 | const Vector >&, Type&); 71 | 72 | 73 | #include "linequs3-impl.h" 74 | 75 | } 76 | // namespace splab 77 | 78 | 79 | #endif 80 | // RANKDEFLINEQUS_H -------------------------------------------------------------------------------- /MatrixFunction/linequs3_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/linequs3_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/linesearch-impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * linesearch-impl.h 28 | * 29 | * Implementation for LineSearch class. 30 | * 31 | * Zhang Ming, 2010-03, Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | /** 36 | * constructors and destructor 37 | */ 38 | template 39 | LineSearch::LineSearch() 40 | { 41 | funcNum = 0; 42 | success = true; 43 | } 44 | 45 | template 46 | LineSearch::~LineSearch() 47 | { 48 | } 49 | 50 | 51 | /** 52 | * Finding the step size at point "xk" in direction of "dk" of function 53 | * "func". The default heuristics number is "maxItr=10". 54 | */ 55 | template 56 | Dtype LineSearch::getStep(Ftype &func, Vector &xk, 57 | Vector &dk, int maxItr) 58 | { 59 | // Set line search parameters that everyone uses. 60 | Dtype mu = Dtype(0.001), 61 | kUp = Dtype(0.5), 62 | kLow = Dtype(0.1), 63 | alpha = Dtype(1.0), 64 | alphaMin, 65 | alphaMax; 66 | 67 | Dtype fNew, 68 | fk = func(xk); 69 | 70 | Vector xNew, 71 | gk = func.grad(xk); 72 | 73 | Dtype gd = dotProd(gk, dk); 74 | 75 | for (int i = 0; i alphaMax) 98 | alpha = alphaMax; 99 | } 100 | } 101 | 102 | if (fNew >= fk) 103 | { 104 | success = false; 105 | return Dtype(0.0); 106 | } 107 | else 108 | { 109 | success = true; 110 | return alpha; 111 | } 112 | } 113 | 114 | 115 | /** 116 | * Get the number of objective function's calculation. 117 | */ 118 | template 119 | inline int LineSearch::getFuncNum() const 120 | { 121 | return funcNum; 122 | } 123 | 124 | 125 | /** 126 | * Judgement whether the optimal solution is found or not. 127 | */ 128 | template 129 | inline bool LineSearch::isSuccess() const 130 | { 131 | return success; 132 | } -------------------------------------------------------------------------------- /MatrixFunction/linesearch.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * linesearch.h 28 | * 29 | * Line search algorithm based on quadratic polynomial interpolation. 30 | * 31 | * This routine is often used in orther optimation method, so it is designed 32 | * as a templated base class. 33 | * 34 | * Zhang Ming, 2010-03, Xi'an Jiaotong University. 35 | *****************************************************************************/ 36 | 37 | 38 | #ifndef LINESEARCH_H 39 | #define LINESEARCH_H 40 | 41 | 42 | #include "vector.h" 43 | 44 | 45 | namespace splab 46 | { 47 | 48 | template 49 | class LineSearch 50 | { 51 | 52 | public: 53 | 54 | LineSearch(); 55 | ~LineSearch(); 56 | 57 | Dtype getStep(Ftype &func, Vector &xk, Vector &dk, 58 | int maxItr = 10); 59 | 60 | int getFuncNum() const; 61 | bool isSuccess() const; 62 | 63 | protected: 64 | 65 | int funcNum; 66 | bool success; 67 | 68 | }; 69 | // class LineSearch 70 | 71 | 72 | #include "linesearch-impl.h" 73 | 74 | } 75 | // namespace splab 76 | 77 | 78 | #endif 79 | // LINESEARCH_H -------------------------------------------------------------------------------- /MatrixFunction/lsfit_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/lsfit_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/lsfitting-impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * lsfitting-impl.h 28 | * 29 | * Implementation for LSFitting class. 30 | * 31 | * Zhang Ming, 2010-04, Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | /** 36 | * constructors and destructor 37 | */ 38 | template 39 | LSFitting::LSFitting(const Vector &xn, const Vector &yn, 40 | Funcs &f) 41 | : Interpolation(xn, yn), phi(f) 42 | { 43 | coefs.resize(phi.dim); 44 | } 45 | 46 | template 47 | LSFitting::~LSFitting() 48 | { 49 | } 50 | 51 | 52 | /** 53 | * Compute fitted parameters. 54 | */ 55 | template 56 | void LSFitting::calcCoefs() 57 | { 58 | int N = xi.size(), 59 | M = coefs.dim(); 60 | 61 | Type tmp; 62 | Vector b(M); 63 | Matrix C(M, M); 64 | 65 | for (int i = 1; i <= M; ++i) 66 | { 67 | tmp = 0; 68 | for (int k = 0; k 94 | Type LSFitting::evaluate(Type x) 95 | { 96 | Type y = 0; 97 | for (int j = 0; j 108 | inline Vector LSFitting::getCoefs() const 109 | { 110 | return coefs; 111 | } -------------------------------------------------------------------------------- /MatrixFunction/lsfitting.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * lsfitting.h 28 | * 29 | * Least Square Fitting. 30 | * 31 | * For a given points set "Pn" {xn,yn} and a fitted function with paramter 32 | * "pm", this class can find the best parameter "pm" in the meaning of least 33 | * mean-square error. 34 | * 35 | * Zhang Ming, 2010-04, Xi'an Jiaotong University. 36 | *****************************************************************************/ 37 | 38 | 39 | #ifndef LSFITTING_H 40 | #define LSFITTING_H 41 | 42 | 43 | #include "matrix.h" 44 | #include "linequs1.h" 45 | #include "fitcurves.h" 46 | #include "interpolation.h" 47 | 48 | 49 | namespace splab 50 | { 51 | 52 | template 53 | class LSFitting : public Interpolation 54 | { 55 | 56 | public: 57 | 58 | using Interpolation::xi; 59 | using Interpolation::yi; 60 | 61 | LSFitting(const Vector &xn, const Vector &yn, 62 | Funcs &f); 63 | ~LSFitting(); 64 | 65 | void calcCoefs(); 66 | Type evaluate(Type x); 67 | Vector getCoefs() const; 68 | 69 | private: 70 | 71 | Vector coefs; // fitted parameters 72 | Funcs phi; // fitted functions 73 | 74 | }; 75 | // class LSFitting 76 | 77 | 78 | #include "lsfitting-impl.h" 79 | 80 | } 81 | // namespace splab 82 | 83 | 84 | #endif 85 | // LSFITTING_H -------------------------------------------------------------------------------- /MatrixFunction/lud-impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * lud-impl.h 28 | * 29 | * Implementation for LUD class. 30 | * 31 | * Zhang Ming, 2010-01 (revised 2010-12), Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | /** 36 | * constructor and destructor 37 | */ 38 | template 39 | LUD::LUD() 40 | { 41 | } 42 | 43 | template 44 | LUD::~LUD() 45 | { 46 | } 47 | 48 | 49 | /** 50 | * permute copy 51 | */ 52 | template 53 | Vector LUD::permuteCopy(const Vector &A, 54 | const Vector &piv) 55 | { 56 | int pivLength = piv.dim(); 57 | if (pivLength != A.dim()) 58 | return Vector(); 59 | 60 | Vector x(pivLength); 61 | for (int i = 0; i 68 | Matrix LUD::permuteCopy(const Matrix &A, 69 | const Vector &piv, int j0, int j1) 70 | { 71 | int pivLength = piv.dim(); 72 | Matrix X(pivLength, j1 - j0 + 1); 73 | 74 | for (int i = 0; i 86 | inline Vector LUD::getPivot() const 87 | { 88 | return piv; 89 | } 90 | 91 | 92 | /** 93 | * LU Decomposition 94 | */ 95 | template 96 | void LUD::dec(const Matrix &A) 97 | { 98 | m = A.rows(); 99 | n = A.cols(); 100 | piv.resize(m); 101 | LU = A; 102 | 103 | // Use a "left-looking", dot-product, Crout/Doolittle algorithm. 104 | for (int i = 0; i LUcolj(m); 110 | 111 | // outer loop 112 | for (int j = 0; j abs(LUcolj[p])) 137 | p = i; 138 | 139 | if (p != j) 140 | { 141 | int k = 0; 142 | for (k = 0; k 161 | Matrix LUD::getL() 162 | { 163 | int p = min(m, n); 164 | Matrix tmp(m, p); 165 | 166 | for (int i = 0; i 180 | MatrixLUD::getU() 181 | { 182 | int p = min(m, n); 183 | Matrix tmp(p, n); 184 | 185 | for (int i = 0; i 197 | Type LUD::det() 198 | { 199 | if (m != n) 200 | return 0; 201 | 202 | Type d = Type(pivsign); 203 | for (int j = 0; j 214 | inline bool LUD::isNonsingular() 215 | { 216 | for (int j = 0; j 229 | Vector LUD::solve(const Vector &b) 230 | { 231 | // dimensions: A is mxn, X is nxk, B is mxk 232 | if (b.dim() != m) 233 | return Vector(); 234 | 235 | if (!isNonsingular()) 236 | return Vector(); 237 | 238 | Vector x = permuteCopy(b, piv); 239 | 240 | // solve L*Y = B(piv) 241 | for (int k = 0; k= 0; --k) 247 | { 248 | x[k] /= LU[k][k]; 249 | for (int i = 0; i 261 | Matrix LUD::solve(const Matrix &B) 262 | { 263 | // dimensions: A is mxn, X is nxk, B is mxk 264 | if (B.rows() != m) 265 | return Matrix(0, 0); 266 | 267 | if (!isNonsingular()) 268 | return Matrix(0, 0); 269 | 270 | // copy right hand side with pivoting 271 | int nx = B.cols(); 272 | Matrix X = permuteCopy(B, piv, 0, nx - 1); 273 | 274 | // solve L*Y = B(piv,:) 275 | for (int k = 0; k= 0; --k) 282 | { 283 | for (int j = 0; j= n, the LU decomposition is an m-by-n 32 | * unit lower triangular matrix L, an n-by-n upper triangular matrix U, and 33 | * a permutation vector piv of length m so that A(piv,:) = L*U. If m < n, 34 | * then L is m-by-m and U is m-by-n. 35 | * 36 | * The matrix can be both REAL or COMPLEX. 37 | * 38 | * The LU decompostion with pivoting always exists, even if the matrix is 39 | * singular, so the constructor will never fail. The primary use of the LU 40 | * decomposition is in the solution of square systems of simultaneouslinear 41 | * equations. This will fail if isNonsingular() returns false. 42 | * 43 | * Adapted form Template Numerical Toolkit. 44 | * 45 | * Zhang Ming, 2010-01 (revised 2010-12), Xi'an Jiaotong University. 46 | *****************************************************************************/ 47 | 48 | 49 | #ifndef LUD_H 50 | #define LUD_H 51 | 52 | 53 | #include "matrix.h" 54 | 55 | 56 | namespace splab 57 | { 58 | 59 | template 60 | class LUD 61 | { 62 | 63 | public: 64 | 65 | LUD(); 66 | ~LUD(); 67 | 68 | void dec(const Matrix &A); 69 | Matrix getL(); 70 | Matrix getU(); 71 | Vector getPivot() const; 72 | 73 | Type det(); 74 | bool isNonsingular(); 75 | 76 | Vector solve(const Vector &b); 77 | Matrix solve(const Matrix &B); 78 | 79 | private: 80 | 81 | // Array for internal storage of decomposition. 82 | Matrix LU; 83 | int m; 84 | int n; 85 | 86 | int pivsign; 87 | Vector piv; 88 | 89 | Vector permuteCopy(const Vector &A, 90 | const Vector &piv); 91 | Matrix permuteCopy(const Matrix &A, 92 | const Vector &piv, int j0, int j1); 93 | 94 | }; 95 | // class LUD 96 | 97 | 98 | #include "lud-impl.h" 99 | 100 | } 101 | // namespace splab 102 | 103 | 104 | #endif 105 | // LUD_H -------------------------------------------------------------------------------- /MatrixFunction/lud_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/lud_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/main_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/main_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/matrix_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/matrix_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/matrixmath-impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * matrixmath-impl.h 28 | * 29 | * Implementation for Matrix math functions. 30 | * 31 | * Zhang Ming, 2010-08, Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | template 36 | Matrix abs(const Matrix &A) 37 | { 38 | int m = A.rows(), 39 | n = A.cols(); 40 | Matrix tmp(m, n); 41 | 42 | for (int i = 0; i 51 | Matrix cos(const Matrix &A) 52 | { 53 | int m = A.rows(), 54 | n = A.cols(); 55 | Matrix tmp(m, n); 56 | 57 | for (int i = 0; i 66 | Matrix sin(const Matrix &A) 67 | { 68 | int m = A.rows(), 69 | n = A.cols(); 70 | Matrix tmp(m, n); 71 | 72 | for (int i = 0; i 81 | Matrix tan(const Matrix &A) 82 | { 83 | int m = A.rows(), 84 | n = A.cols(); 85 | Matrix tmp(m, n); 86 | 87 | for (int i = 0; i 96 | Matrix acos(const Matrix &A) 97 | { 98 | int m = A.rows(), 99 | n = A.cols(); 100 | Matrix tmp(m, n); 101 | 102 | for (int i = 0; i 111 | Matrix asin(const Matrix &A) 112 | { 113 | int m = A.rows(), 114 | n = A.cols(); 115 | Matrix tmp(m, n); 116 | 117 | for (int i = 0; i 126 | Matrix atan(const Matrix &A) 127 | { 128 | int m = A.rows(), 129 | n = A.cols(); 130 | Matrix tmp(m, n); 131 | 132 | for (int i = 0; i 141 | Matrix exp(const Matrix &A) 142 | { 143 | int m = A.rows(), 144 | n = A.cols(); 145 | Matrix tmp(m, n); 146 | 147 | for (int i = 0; i 156 | Matrix log(const Matrix &A) 157 | { 158 | int m = A.rows(), 159 | n = A.cols(); 160 | Matrix tmp(m, n); 161 | 162 | for (int i = 0; i 171 | Matrix log10(const Matrix &A) 172 | { 173 | int m = A.rows(), 174 | n = A.cols(); 175 | Matrix tmp(m, n); 176 | 177 | for (int i = 0; i 186 | Matrix sqrt(const Matrix &A) 187 | { 188 | int m = A.rows(), 189 | n = A.cols(); 190 | Matrix tmp(m, n); 191 | 192 | for (int i = 0; i 201 | Matrix pow(const Matrix &B, const Matrix &E) 202 | { 203 | int m = B.rows(), 204 | n = B.cols(); 205 | assert(m == E.rows()); 206 | assert(n == E.cols()); 207 | 208 | Matrix tmp(m, n); 209 | for (int i = 0; i 218 | Matrix pow(const Matrix &B, const Type &e) 219 | { 220 | int m = B.rows(), 221 | n = B.cols(); 222 | Matrix tmp(m, n); 223 | 224 | for (int i = 0; i 233 | Matrix pow(const Type &b, const Matrix &E) 234 | { 235 | int m = E.rows(), 236 | n = E.cols(); 237 | Matrix tmp(m, n); 238 | 239 | for (int i = 0; i Matrix abs(const Matrix&); 52 | template Matrix cos(const Matrix&); 53 | template Matrix sin(const Matrix&); 54 | template Matrix tan(const Matrix&); 55 | template Matrix acos(const Matrix&); 56 | template Matrix asin(const Matrix&); 57 | template Matrix atan(const Matrix&); 58 | 59 | template Matrix exp(const Matrix&); 60 | template Matrix log(const Matrix&); 61 | template Matrix log10(const Matrix&); 62 | 63 | template Matrix sqrt(const Matrix&); 64 | template Matrix pow(const Matrix&, 65 | const Matrix&); 66 | template Matrix pow(const Matrix&, 67 | const Type&); 68 | template Matrix pow(const Type&, 69 | const Matrix&); 70 | 71 | 72 | #include "matrixmath-impl.h" 73 | 74 | } 75 | // namespace splab 76 | 77 | 78 | #endif 79 | // MATRIXMATH_H -------------------------------------------------------------------------------- /MatrixFunction/matrixmath_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/matrixmath_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/newtoninterp-impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * newtoninterp-impl.h 28 | * 29 | * Implementation for NewtonInterp class. 30 | * 31 | * Zhang Ming, 2010-04, Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | /** 36 | * constructors and destructor 37 | */ 38 | template 39 | NewtonInterp::NewtonInterp(const Vector &xn, 40 | const Vector &yn) 41 | : Interpolation(xn, yn), coefs(yn) 42 | { 43 | } 44 | 45 | template 46 | NewtonInterp::~NewtonInterp() 47 | { 48 | } 49 | 50 | 51 | /** 52 | * Compute polynomial' coefsficients. 53 | */ 54 | template 55 | void NewtonInterp::calcCoefs() 56 | { 57 | int N = xi.size(); 58 | for (int j = 1; j= j; --i) 60 | coefs[i] = (coefs[i] - coefs[i - 1]) / (xi[i] - xi[i - j]); 61 | } 62 | 63 | 64 | /** 65 | * Compute the value of polynomial at given "x". 66 | */ 67 | template 68 | Type NewtonInterp::evaluate(Type x) 69 | { 70 | int N = xi.size(); 71 | Type y = 0, 72 | tmp = 0; 73 | 74 | for (int j = 0; j 91 | inline Vector NewtonInterp::getCoefs() const 92 | { 93 | return coefs; 94 | } -------------------------------------------------------------------------------- /MatrixFunction/newtoninterp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * newtoninterp.h 28 | * 29 | * Newton interpolation method. 30 | * 31 | * For a given points set "Pn" {xn,yn}, this class can find a polynomial which 32 | * cross all points of "Pn". 33 | * 34 | * Zhang Ming, 2010-04, Xi'an Jiaotong University. 35 | *****************************************************************************/ 36 | 37 | 38 | #ifndef NEWTONINTERP_H 39 | #define NEWTONINTERP_H 40 | 41 | 42 | #include "interpolation.h" 43 | 44 | 45 | namespace splab 46 | { 47 | 48 | template 49 | class NewtonInterp : public Interpolation 50 | { 51 | 52 | public: 53 | 54 | using Interpolation::xi; 55 | using Interpolation::yi; 56 | 57 | NewtonInterp(const Vector &xn, const Vector &yn); 58 | ~NewtonInterp(); 59 | 60 | void calcCoefs(); 61 | Type evaluate(Type x); 62 | Vector getCoefs() const; 63 | 64 | private: 65 | 66 | Vector coefs; 67 | 68 | }; 69 | // class NewtonInterp 70 | 71 | 72 | #include "newtoninterp-impl.h" 73 | 74 | } 75 | // namespace splab 76 | 77 | 78 | #endif 79 | // NEWTONINTERP_H -------------------------------------------------------------------------------- /MatrixFunction/newtoninterp_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/newtoninterp_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/nleroot-impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * nleroot-impl.h 28 | * 29 | * Implementation for nonlinear equation rooting. 30 | * 31 | * Zhang Ming, 2010-04, Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | /** 36 | * Bisection method for finding function root. 37 | */ 38 | template 39 | Type bisection(NLFunc &f, Type a, Type b, Type tol) 40 | { 41 | Type c = 0, 42 | fc = 0, 43 | fa = f(a), 44 | fb = f(b); 45 | 46 | if (a > b) 47 | { 48 | c = a; 49 | a = b; 50 | b = c; 51 | } 52 | if (fa*fb > 0) 53 | { 54 | cerr << " Invalid interval!" << endl; 55 | return Type(0); 56 | } 57 | 58 | int maxItr = (int)ceil((log(b - a) - log(tol)) / log(2.0)); 59 | for (int i = 0; i 0) 68 | { 69 | b = c; 70 | fb = fc; 71 | } 72 | else 73 | { 74 | a = c; 75 | fa = fc; 76 | } 77 | 78 | if ((b - a) < tol) 79 | return (b + a) / 2; 80 | } 81 | 82 | cout << "No solution for the specified tolerance!" << endl; 83 | return c; 84 | } 85 | 86 | 87 | /** 88 | * Newton method for finding function root. 89 | */ 90 | template 91 | Type newton(NLFunc &f, Type x0, Type tol, int maxItr) 92 | { 93 | Type xkOld = x0, 94 | xkNew, 95 | fkGrad; 96 | 97 | for (int i = 0; i 124 | Type secant(NLFunc &f, Type x1, Type x2, Type tol, int maxItr) 125 | { 126 | Type x_k, 127 | x_k1 = x1, 128 | x_k2 = x2, 129 | f_k1 = f(x_k1), 130 | f_k2; 131 | 132 | for (int i = 0; i 48 | #include "constants.h" 49 | #include "nlfunc.h" 50 | 51 | 52 | using namespace std; 53 | 54 | 55 | namespace splab 56 | { 57 | 58 | template Type bisection(NLFunc &f, Type a, Type b, 59 | Type tol = Type(1.0e-6)); 60 | 61 | template Type newton(NLFunc &f, Type x0, 62 | const Type tol = Type(1.0e-6), 63 | int maxItr = MAXTERM); 64 | 65 | template Type secant(NLFunc &f, Type x1, Type x2, 66 | const Type tol = Type(1.0e-6), 67 | int maxItr = MAXTERM); 68 | 69 | 70 | #include "nleroot-impl.h" 71 | 72 | } 73 | // namespace splab 74 | 75 | 76 | #endif 77 | // NLEROOT_H -------------------------------------------------------------------------------- /MatrixFunction/nleroot_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/nleroot_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/nleroots-impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * nleroots-impl.h 28 | * 29 | * Implementation for nonlinear equations rooting. 30 | * 31 | * Zhang Ming, 2010-10, Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | /** 36 | * Bisection method for finding function root. 37 | */ 38 | template 39 | Vector seidel(NLEqus &G, const Vector &X0, 40 | const Type tol, int maxItr) 41 | { 42 | int N = X0.dim(); 43 | Vector X(X0), XNew(X0), tmp(N); 44 | 45 | for (int k = 0; k 72 | Vector newton(NLFuncs &F, const Vector &X0, 73 | const Type tol, const Type eps, int maxItr) 74 | { 75 | Vector X(X0), XNew(X0.dim()); 76 | 77 | for (int k = 0; k 54 | Vector seidel(NLEqus &G, const Vector &X0, 55 | const Type tol = Type(1.0e-6), 56 | int maxItr = MAXTERM); 57 | 58 | template 59 | Vector newton(NLFuncs &F, const Vector &X0, 60 | const Type tol = Type(1.0e-6), 61 | const Type eps = Type(1.0e-9), 62 | int maxItr = MAXTERM); 63 | 64 | 65 | #include "nleroots-impl.h" 66 | 67 | } 68 | // namespace splab 69 | 70 | 71 | #endif 72 | // NLEROOTS_H -------------------------------------------------------------------------------- /MatrixFunction/nleroots_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/nleroots_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/nlfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * nlfunc.h 28 | * 29 | * Nonlinear function. 30 | * 31 | * Zhang Ming, 2010-04, Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | #ifndef NLFUNC_H 36 | #define NLFUNC_H 37 | 38 | 39 | namespace splab 40 | { 41 | 42 | template 43 | class NLFunc 44 | { 45 | 46 | public: 47 | 48 | /** 49 | * Initialize the parameters 50 | */ 51 | NLFunc(Type aa, Type bb, Type cc) : a(aa), b(bb), c(cc) 52 | { } 53 | 54 | /** 55 | * Compute the value of objective function at point x. 56 | */ 57 | Type operator()(Type &x) 58 | { 59 | return (a*x*x + b*x + c); 60 | } 61 | 62 | /** 63 | * Compute the gradient of objective function at point x. 64 | */ 65 | Type grad(Type &x) 66 | { 67 | return (2 * a*x + b); 68 | } 69 | 70 | private: 71 | 72 | // parameters 73 | Type a, 74 | b, 75 | c; 76 | 77 | }; 78 | // class NLFunc 79 | 80 | } 81 | // namespace splab 82 | 83 | 84 | #endif 85 | // NLFUNC_H -------------------------------------------------------------------------------- /MatrixFunction/nlfuncs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * nlfuncs.h 28 | * 29 | * Nonlinear equations and functions. 30 | * 31 | * Zhang Ming, 2010-10, Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | #ifndef NLFUNCS_H 36 | #define NLFUNCS_H 37 | 38 | 39 | #include "vector.h" 40 | #include "matrix.h" 41 | 42 | 43 | namespace splab 44 | { 45 | 46 | template 47 | class NLEqus 48 | { 49 | 50 | public: 51 | 52 | /** 53 | * Compute the values of equations at point X. 54 | */ 55 | Vector operator()(const Vector &X) 56 | { 57 | Vector XNew(X.dim()); 58 | 59 | XNew(1) = (X(1)*X(1) - X(2) + Type(0.5)) / 2; 60 | XNew(2) = (-X(1)*X(1) - 4 * X(2)*X(2) + 8 * X(2) + 4) / 8; 61 | 62 | return XNew; 63 | } 64 | 65 | }; 66 | // class NLEqus 67 | 68 | 69 | template 70 | class NLFuncs 71 | { 72 | 73 | public: 74 | 75 | /** 76 | * Compute the values of functions at point X. 77 | */ 78 | Vector operator()(Vector &X) 79 | { 80 | Vector FX(X.dim()); 81 | 82 | FX(1) = X(1)*X(1) - 2 * X(1) - X(2) + Type(0.5); 83 | FX(2) = X(1)*X(1) + 4 * X(2)*X(2) - 4; 84 | 85 | return FX; 86 | } 87 | 88 | /** 89 | * Compute the Jacobian-Matrix at point X. 90 | */ 91 | Matrix jacobi(Vector &X) 92 | { 93 | Matrix JX(X.dim(), X.dim()); 94 | 95 | JX(1, 1) = 2 * X(1) - 2; JX(1, 2) = Type(-1); 96 | JX(2, 1) = 2 * X(1); JX(2, 2) = 8 * X(2); 97 | 98 | return JX; 99 | } 100 | 101 | }; 102 | // class NLFuncs 103 | 104 | } 105 | // namespace splab 106 | 107 | 108 | #endif 109 | // NLFUNCS_H -------------------------------------------------------------------------------- /MatrixFunction/objfunc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * objfunc.h 28 | * 29 | * Function Object. 30 | * 31 | * We can use this functor computing the value of objective function and it's 32 | * gradient vector. The objective function is supposed to be multidimensional, 33 | * one dimention is the special case of "vector.dim()=1". 34 | * 35 | * Zhang Ming, 2010-03, Xi'an Jiaotong University. 36 | *****************************************************************************/ 37 | 38 | 39 | #ifndef OBJFUNC_H 40 | #define OBJFUNC_H 41 | 42 | 43 | #include "vector.h" 44 | 45 | 46 | namespace splab 47 | { 48 | 49 | template 50 | class ObjFunc 51 | { 52 | 53 | public: 54 | 55 | /** 56 | * Initialize the parameters 57 | */ 58 | ObjFunc(Type aa, Type bb, Type cc) : a(aa), b(bb), c(cc) 59 | { } 60 | 61 | /** 62 | * Compute the value of objective function at point x. 63 | */ 64 | Type operator()(Vector &x) 65 | { 66 | return (a*x(1) * exp(b*x(1)*x(1) + c*x(2)*x(2))); 67 | } 68 | 69 | /** 70 | * Compute the gradient of objective function at point x. 71 | */ 72 | Vector grad(Vector &x) 73 | { 74 | Type expTerm = exp(a*x(1)*x(1) + b*x(2)*x(2)); 75 | Vector df(x.dim()); 76 | df(1) = (a + 2 * a*b*x(1)*x(1)) * expTerm; 77 | df(2) = 2 * a*c*x(1)*x(2) * expTerm; 78 | 79 | return df; 80 | } 81 | 82 | private: 83 | 84 | // parameters 85 | Type a, 86 | b, 87 | c; 88 | 89 | }; 90 | // class ObjFunc 91 | 92 | } 93 | // namespace splab 94 | 95 | 96 | #endif 97 | // OBJFUNC_H -------------------------------------------------------------------------------- /MatrixFunction/pseudoinverse-impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * pseudoinverse-impl.h 28 | * 29 | * Implementation for matrix pseudoinverse 30 | * 31 | * Zhang Ming, 2010-08 (revised 2010-12), Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | /** 36 | * Compute the pseudoinverse of a real matrix. 37 | */ 38 | template 39 | Matrix pinv(const Matrix &A, Real tol) 40 | { 41 | int m = A.rows(), 42 | n = A.cols(); 43 | 44 | SVD svd; 45 | svd.dec(A); 46 | Matrix U = svd.getU(); 47 | Matrix V = svd.getV(); 48 | Vector s = svd.getSV(); 49 | 50 | int r = 0; 51 | if (tol <= 0) 52 | tol = max(m, n) * s[0] * EPS; 53 | for (int i = 0; i= tol) 55 | r++; 56 | 57 | for (int i = 0; i invA(n, m); 62 | for (int i = 0; i 79 | Matrix > pinv2(const Matrix > &A, Type tol) 80 | { 81 | int m = A.rows(), 82 | n = A.cols(); 83 | 84 | CSVD svd; 85 | svd.dec(A); 86 | Matrix > U = svd.getU(); 87 | Matrix > V = svd.getV(); 88 | Vector s = svd.getSV(); 89 | 90 | int r = 0; 91 | if (tol <= 0) 92 | tol = max(m, n) * s[0] * EPS; 93 | for (int i = 0; i= tol) 95 | r++; 96 | 97 | for (int i = 0; i >invA(n, m); 102 | for (int i = 0; i sum = 0; 106 | for (int k = 0; k 56 | Matrix pinv(const Matrix&, Real tol = Real(-1.0)); 57 | 58 | template 59 | Matrix > pinv2(const Matrix >&, 60 | Type tol = Type(-1.0)); 61 | 62 | 63 | #include "pseudoinverse-impl.h" 64 | } 65 | // namespace splab 66 | 67 | 68 | #endif 69 | // PSEUDOINVERSE_H -------------------------------------------------------------------------------- /MatrixFunction/pseudoinverse_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/pseudoinverse_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/qrd-impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * qrd-impl.h 28 | * 29 | * Implementation for QRD class. 30 | * 31 | * Zhang Ming, 2010-01 (revised 2010-12), Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | /** 36 | * constructor and destructor 37 | */ 38 | template 39 | QRD::QRD() 40 | { 41 | } 42 | 43 | template 44 | QRD::~QRD() 45 | { 46 | } 47 | 48 | 49 | /** 50 | * Create a QR factorization object for A. 51 | */ 52 | template 53 | void QRD::dec(const Matrix &A) 54 | { 55 | 56 | int m = A.rows(), 57 | n = A.cols(), 58 | p = min(m, n); 59 | QR = A; 60 | RDiag = Vector(p); 61 | 62 | // main loop. 63 | for (int k = 0; k 106 | inline bool QRD::isFullRank() const 107 | { 108 | for (int j = 0; j 120 | Matrix QRD::getQ() 121 | { 122 | int m = QR.rows(), 123 | p = RDiag.dim(); 124 | Matrix Q(m, p); 125 | 126 | for (int k = p - 1; k >= 0; --k) 127 | { 128 | for (int i = 0; i 153 | Matrix QRD::getR() 154 | { 155 | int n = QR.cols(), 156 | p = RDiag.dim(); 157 | Matrix R(p, n); 158 | 159 | for (int i = 0; i 174 | Matrix QRD::getH() 175 | { 176 | int m = QR.rows(), 177 | p = RDiag.dim(); 178 | Matrix H(m, p); 179 | 180 | for (int i = 0; i 195 | Vector QRD::solve(const Vector &b) 196 | { 197 | int m = QR.rows(), 198 | n = QR.cols(); 199 | 200 | assert(b.dim() == m); 201 | 202 | // matrix is rank deficient 203 | if (!isFullRank()) 204 | return Vector(); 205 | 206 | Vector x = b; 207 | 208 | // compute y = transpose(Q)*b 209 | for (int k = 0; k= 0; --k) 222 | { 223 | x[k] /= RDiag[k]; 224 | for (int i = 0; i x_(n); 230 | for (int i = 0; i 244 | Matrix QRD::solve(const Matrix &B) 245 | { 246 | int m = QR.rows(); 247 | int n = QR.cols(); 248 | 249 | assert(B.rows() == m); 250 | 251 | // matrix is rank deficient 252 | if (!isFullRank()) 253 | return Matrix(0, 0); 254 | 255 | int nx = B.cols(); 256 | Matrix X = B; 257 | 258 | // compute Y = transpose(Q)*B 259 | for (int k = 0; k= 0; --k) 273 | { 274 | for (int j = 0; j X_(n, nx); 284 | for (int i = 0; i 58 | class QRD 59 | { 60 | 61 | public: 62 | 63 | QRD(); 64 | ~QRD(); 65 | 66 | void dec(const Matrix &A); 67 | bool isFullRank() const; 68 | 69 | Matrix getQ(); 70 | Matrix getR(); 71 | Matrix getH(); 72 | 73 | Vector solve(const Vector &b); 74 | Matrix solve(const Matrix &B); 75 | 76 | private: 77 | 78 | // internal storage of QR 79 | Matrix QR; 80 | 81 | // diagonal of R. 82 | Vector RDiag; 83 | 84 | }; 85 | // class QRD 86 | 87 | 88 | #include "qrd-impl.h" 89 | 90 | } 91 | // namespace splab 92 | 93 | 94 | #endif 95 | // QRD_H -------------------------------------------------------------------------------- /MatrixFunction/qrd_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/qrd_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/spline3interp-impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * spline3interp-impl.h 28 | * 29 | * Implementation for Spline3Interp class. 30 | * 31 | * Zhang Ming, 2010-04, Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | /** 36 | * constructors and destructor 37 | */ 38 | template 39 | Spline3Interp::Spline3Interp(const Vector &xn, 40 | const Vector &yn, 41 | Type d2l, Type d2r) 42 | : Interpolation(xn, yn), M0(d2l), Mn(d2r) 43 | { 44 | } 45 | 46 | template 47 | Spline3Interp::~Spline3Interp() 48 | { 49 | } 50 | 51 | 52 | /** 53 | * Compute the second derivative at interpolated points. 54 | */ 55 | template 56 | void Spline3Interp::derivative2(Vector &dx, Vector &d1, 57 | Vector &d2) 58 | { 59 | int N = xi.size(), 60 | M = N - 1; 61 | Vector b(M), 62 | v(M), 63 | y(M), 64 | alpha(M), 65 | beta(M - 1); 66 | 67 | for (int i = 1; i0; --i) 88 | d2[i] = y[i] - beta[i] * d2[i + 1]; 89 | } 90 | 91 | 92 | /** 93 | * Compute the polynomial' coefsficient in each interval. 94 | */ 95 | template 96 | void Spline3Interp::calcCoefs() 97 | { 98 | int N = xi.size(), 99 | M = N - 1; 100 | 101 | Vector m(N), 102 | h(M), 103 | d(M); 104 | 105 | m[0] = M0; 106 | m[M] = Mn; 107 | for (int i = 0; i 130 | Type Spline3Interp::evaluate(Type x) 131 | { 132 | int k = -1, 133 | N = xi.size(), 134 | M = N - 1; 135 | 136 | Type dx, 137 | y; 138 | 139 | for (int i = 0; i= x)) 142 | { 143 | k = i; 144 | dx = x - xi[i]; 145 | break; 146 | } 147 | } 148 | if (k != -1) 149 | { 150 | y = ((coefs[k][3] * dx + coefs[k][2]) * dx + coefs[k][1]) * dx 151 | + coefs[k][0]; 152 | return y; 153 | } 154 | else 155 | { 156 | cerr << "The value is out of range!" << endl; 157 | return Type(0); 158 | } 159 | } 160 | 161 | 162 | /** 163 | * Get polynomial' coefsficients. 164 | */ 165 | template 166 | inline Matrix Spline3Interp::getCoefs() const 167 | { 168 | return coefs; 169 | } -------------------------------------------------------------------------------- /MatrixFunction/spline3interp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * spline3interp.h 28 | * 29 | * Cubic splines interpolation method. 30 | * 31 | * For a given points set "Pn" {xn,yn}, this class can find a cubic polynomial 32 | * in each interval [x_i, x_i+1], such that both of the polynomials and their 33 | * first order derivative are continuous at the bound of each interval. 34 | * 35 | * Zhang Ming, 2010-04, Xi'an Jiaotong University. 36 | *****************************************************************************/ 37 | 38 | 39 | #ifndef SPLINE3INTERP_H 40 | #define SPLINE3INTERP_H 41 | 42 | 43 | #include "matrix.h" 44 | #include "interpolation.h" 45 | 46 | 47 | namespace splab 48 | { 49 | 50 | template 51 | class Spline3Interp : public Interpolation 52 | { 53 | 54 | public: 55 | 56 | using Interpolation::xi; 57 | using Interpolation::yi; 58 | 59 | Spline3Interp(const Vector &xn, const Vector &yn, 60 | Type d2l = Type(0), Type d2r = Type(0)); 61 | ~Spline3Interp(); 62 | 63 | void calcCoefs(); 64 | Type evaluate(Type x); 65 | Matrix getCoefs() const; 66 | 67 | private: 68 | 69 | void derivative2(Vector &dx, Vector &d1, 70 | Vector &d2); 71 | 72 | Type M0, 73 | Mn; 74 | Matrix coefs; 75 | 76 | }; 77 | // class Spline3Interp 78 | 79 | 80 | #include "spline3interp-impl.h" 81 | 82 | } 83 | // namespace splab 84 | 85 | 86 | #endif 87 | // SPLINE3INTERP_H -------------------------------------------------------------------------------- /MatrixFunction/spline3interp_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/spline3interp_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/steepdesc-impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * steepdesc-impl.h 28 | * 29 | * Implementation for SteepDesc class. 30 | * 31 | * Zhang Ming, 2010-03, Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | /** 36 | * constructors and destructor 37 | */ 38 | template 39 | SteepDesc::SteepDesc() : LineSearch() 40 | { 41 | } 42 | 43 | template 44 | SteepDesc::~SteepDesc() 45 | { 46 | } 47 | 48 | 49 | /** 50 | * Finding the optimal solution. The default tolerance error and maximum 51 | * iteratin number are "tol=1.0e-6" and "maxItr=100", respectively. 52 | */ 53 | template 54 | void SteepDesc::optimize(Ftype &func, Vector &x0, 55 | Dtype tol, int maxItr) 56 | { 57 | // initialize parameters. 58 | int k = 0; 59 | Vector x(x0); 60 | Dtype fx = func(x); 61 | this->funcNum++; 62 | Vector gnorm(maxItr); 63 | Vector g = func.grad(x); 64 | gnorm[k++] = norm(g); 65 | 66 | Dtype alpha; 67 | Vector d(g.dim()); 68 | 69 | while ((gnorm(k) > tol) && (k < maxItr)) 70 | { 71 | // descent direction 72 | d = -g; 73 | 74 | // one dimension searching 75 | alpha = this->getStep(func, x, d); 76 | if (!this->success) 77 | break; 78 | 79 | // update 80 | x += alpha*d; 81 | fx = func(x); 82 | this->funcNum++; 83 | g = func.grad(x); 84 | gnorm[k++] = norm(g); 85 | } 86 | 87 | xOpt = x; 88 | fMin = fx; 89 | gradNorm.resize(k); 90 | for (int i = 0; i tol) 94 | this->success = false; 95 | } 96 | 97 | 98 | /** 99 | * Get the optimum point. 100 | */ 101 | template 102 | inline Vector SteepDesc::getOptValue() const 103 | { 104 | return xOpt; 105 | } 106 | 107 | 108 | /** 109 | * Get the norm of gradient in each iteration. 110 | */ 111 | template 112 | inline Vector SteepDesc::getGradNorm() const 113 | { 114 | return gradNorm; 115 | } 116 | 117 | 118 | /** 119 | * Get the minimum value of objective function. 120 | */ 121 | template 122 | inline Dtype SteepDesc::getFuncMin() const 123 | { 124 | return fMin; 125 | } 126 | 127 | 128 | /** 129 | * Get the iteration number. 130 | */ 131 | template 132 | inline int SteepDesc::getItrNum() const 133 | { 134 | return gradNorm.dim() - 1; 135 | } -------------------------------------------------------------------------------- /MatrixFunction/steepdesc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * steepdesc.h 28 | * 29 | * Steepest descent optimal method( gradient algorithm ). 30 | * 31 | * This class is designed for finding the minimum value of objective function 32 | * in one dimension or multidimension. Inexact line search algorithm is used 33 | * to get the step size in each iteration. 34 | * 35 | * Zhang Ming, 2010-03, Xi'an Jiaotong University. 36 | *****************************************************************************/ 37 | 38 | 39 | #ifndef STEEPDESC_H 40 | #define STEEPDESC_H 41 | 42 | 43 | #include "linesearch.h" 44 | 45 | 46 | namespace splab 47 | { 48 | 49 | template 50 | class SteepDesc : public LineSearch 51 | { 52 | 53 | public: 54 | 55 | SteepDesc(); 56 | ~SteepDesc(); 57 | 58 | void optimize(Ftype &func, Vector &x0, 59 | Dtype tol = Dtype(1.0e-6), int maxItr = 100); 60 | 61 | Vector getOptValue() const; 62 | Vector getGradNorm() const; 63 | Dtype getFuncMin() const; 64 | int getItrNum() const; 65 | 66 | private: 67 | 68 | // iteration number 69 | int itrNum; 70 | 71 | // minimum value of objective function 72 | Dtype fMin; 73 | 74 | // optimal solution 75 | Vector xOpt; 76 | 77 | // gradient norm for each iteration 78 | Vector gradNorm; 79 | 80 | }; 81 | // class SteepDesc 82 | 83 | 84 | #include "steepdesc-impl.h" 85 | 86 | } 87 | // namespace splab 88 | 89 | 90 | #endif 91 | // STEEPDESC_H -------------------------------------------------------------------------------- /MatrixFunction/steepdesc_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/steepdesc_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/svd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * svd.h 28 | * 29 | * Class template of Singular Value Decomposition. 30 | * 31 | * For an m-by-n matrix A, the singular value decomposition is an m-by-m 32 | * orthogonal matrix U, an m-by-n diagonal matrix S, and an n-by-n orthogonal 33 | * matrix V so that A = U*S*V^T. 34 | * 35 | * The singular values, sigma[k] = S[k][k], are ordered so that sigma[0] >= 36 | * sigma[1] >= ... >= sigma[n-1]. 37 | * 38 | * For economy size, denotes p = min(m,n), then U is m-by-p, S is p-by-p, 39 | * and V is n-by-p, this file provides the economic decomposition format. 40 | * 41 | * The singular value decompostion always exists, so the constructor will 42 | * never fail. The matrix condition number and the effective numerical rank 43 | * can be computed from this decomposition. 44 | * 45 | * Adapted form Template Numerical Toolkit. 46 | * 47 | * Zhang Ming, 2010-01 (revised 2010-08), Xi'an Jiaotong University. 48 | *****************************************************************************/ 49 | 50 | 51 | #ifndef SVD_H 52 | #define SVD_H 53 | 54 | 55 | #include "matrix.h" 56 | 57 | 58 | namespace splab 59 | { 60 | 61 | template 62 | class SVD 63 | { 64 | 65 | public: 66 | 67 | SVD(); 68 | ~SVD(); 69 | 70 | void dec(const Matrix &A); 71 | Matrix getU() const; 72 | Matrix getV() const; 73 | Matrix getSM(); 74 | Vector getSV() const; 75 | 76 | Real norm2() const; 77 | Real cond() const; 78 | int rank(); 79 | 80 | private: 81 | 82 | // the orthogonal matrix and singular value vector 83 | Matrix U; 84 | Matrix V; 85 | Vector S; 86 | 87 | // docomposition for matrix with rows >= columns 88 | void decomposition(Matrix&, Matrix&, 89 | Vector&, Matrix&); 90 | 91 | }; 92 | // class SVD 93 | 94 | 95 | #include "svd-impl.h" 96 | 97 | } 98 | // namespace splab 99 | 100 | 101 | #endif 102 | // SVD_H -------------------------------------------------------------------------------- /MatrixFunction/svd_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/svd_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/usingdeclare.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * usingdeclare.h 28 | * 29 | * Usually used name declaration. 30 | * 31 | * Zhang Ming, 2010-08, Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | #ifndef USINGDECLARE_H 36 | #define USINGDECLARE_H 37 | 38 | 39 | namespace splab 40 | { 41 | 42 | using std::cin; 43 | using std::cout; 44 | using std::cerr; 45 | using std::endl; 46 | 47 | using std::string; 48 | using std::complex; 49 | 50 | using std::ostream; 51 | using std::istream; 52 | 53 | using std::min; 54 | using std::max; 55 | using std::swap; 56 | using std::ceil; 57 | 58 | using std::abs; 59 | using std::cos; 60 | using std::sin; 61 | using std::tan; 62 | using std::acos; 63 | using std::asin; 64 | using std::atan; 65 | using std::exp; 66 | using std::log; 67 | using std::log10; 68 | using std::sqrt; 69 | using std::pow; 70 | 71 | } 72 | // namespace splab 73 | 74 | 75 | #endif 76 | // USINGDECLARE_H -------------------------------------------------------------------------------- /MatrixFunction/utilities.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * utilities.h 28 | * 29 | * Some usable routines converted from "Matlab", which are used in wavelet 30 | * transform and time-frequency analysis, such as "filp"(same to reverse), 31 | * "shift", "circshift", "fftshift", "dyadup", "wkeep", "wextend" and so on. 32 | * 33 | * Zhang Ming, 2010-01, Xi'an Jiaotong University. 34 | *****************************************************************************/ 35 | 36 | 37 | #ifndef UTILITIES_H 38 | #define UTILITIES_H 39 | 40 | 41 | #include 42 | #include "vector.h" 43 | #include "fft.h" 44 | 45 | 46 | namespace splab 47 | { 48 | 49 | int mod( int, int ); 50 | int ceil( int, int ); 51 | 52 | template Vector reverse( const Vector& ); 53 | template Vector flip( const Vector& ); 54 | template Vector shift( const Vector& ); 55 | template Vector cirshift( const Vector& ); 56 | template Vector fftshift( const Vector& ); 57 | 58 | template Vector dyadUp( const Vector&, int ); 59 | template Vector dyadDown( const Vector&, int ); 60 | template Vector fftInterp( const Vector&, int ); 61 | template 62 | Vector< complex > fftInterp( const Vector< complex >&, int ); 63 | 64 | template 65 | Vector wkeep( const Vector&, int, int ); 66 | template 67 | Vector wkeep( const Vector&, int, 68 | const string &direction="center" ); 69 | template 70 | Vector wextend( const Vector&, int, 71 | const string &direction="both", 72 | const string &mode="zpd" ); 73 | 74 | 75 | #include "utilities-impl.h" 76 | 77 | } 78 | // namespace splab 79 | 80 | 81 | #endif 82 | // UTILITIES_H 83 | -------------------------------------------------------------------------------- /MatrixFunction/utilities_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/utilities_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/vector.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * vector.h 28 | * 29 | * Class template of vector which is designed for basic linear algebra 30 | * operations such as: 31 | * v + k k + v v += k v1 + v2 v1 += v2 32 | * v - k k - v v -= k v1 - v2 v1 -= v2 33 | * v * k k * v v *= k v1 * v2 v1 *= v2 34 | * v / k k / v v /= k v1 / v2 v1 /= v2 35 | * mum, min, max swap reverse 36 | * norm dotProd 37 | * These operators and functions can be applied to both real vector and 38 | * complex vector. 39 | * 40 | * The class also provides the basic math functions such as: 41 | * cos sin tan acos asin atan 42 | * abs exp log log10 sqrt pow 43 | * This should include "matrixmath.h" file. 44 | * 45 | * When debugging, use #define BOUNDS_CHECK above your "#include vector.h" 46 | * line. When done debugging, comment out #define BOUNDS_CHECK for better 47 | * performance. 48 | * 49 | * Zhang Ming, 2010-01 (revised 2010-12), Xi'an Jiaotong University. 50 | *****************************************************************************/ 51 | 52 | 53 | #ifndef VECTOR_H 54 | #define VECTOR_H 55 | 56 | 57 | #include 58 | #include 59 | #include 60 | #include 61 | #include 62 | #include "usingdeclare.h" 63 | #include "constants.h" 64 | 65 | 66 | namespace splab 67 | { 68 | 69 | template 70 | class Vector 71 | { 72 | 73 | public: 74 | 75 | typedef Type* iterator; 76 | typedef const Type* const_iterator; 77 | 78 | // constructors and destructor 79 | Vector(); 80 | Vector(const Vector &v); 81 | Vector(int length, const Type &x = Type(0)); 82 | Vector(int length, const Type *array); 83 | ~Vector(); 84 | 85 | // assignments 86 | Vector& operator=(const Vector &v); 87 | Vector& operator=(const Type &x); 88 | 89 | // accessors 90 | Type& operator[](int i); 91 | const Type& operator[](int i) const; 92 | Type& operator()(int i); 93 | const Type& operator()(int i) const; 94 | 95 | // iterators 96 | iterator begin(); 97 | const_iterator begin() const; 98 | iterator end(); 99 | const_iterator end() const; 100 | 101 | // type conversion 102 | operator Type*(); 103 | operator const Type*() const; 104 | 105 | // others 106 | int size() const; 107 | int dim() const; 108 | Vector& resize(int length); 109 | 110 | // computed assignment 111 | Vector& operator+=(const Type&); 112 | Vector& operator-=(const Type&); 113 | Vector& operator*=(const Type&); 114 | Vector& operator/=(const Type&); 115 | Vector& operator+=(const Vector&); 116 | Vector& operator-=(const Vector&); 117 | Vector& operator*=(const Vector&); 118 | Vector& operator/=(const Vector&); 119 | 120 | private: 121 | 122 | // data pointer for 0-offset indexing 123 | Type *pv0; 124 | 125 | // data pointer for 1-offset indexing 126 | Type *pv1; 127 | 128 | // the row number of vector 129 | int nRow; 130 | 131 | void init(int length); 132 | void copyFromArray(const Type *v); 133 | void setByScalar(const Type &x); 134 | void destroy(); 135 | 136 | }; 137 | // class Vector 138 | 139 | 140 | // input and output 141 | template 142 | ostream& operator<<(ostream&, const Vector&); 143 | template 144 | istream& operator>>(istream&, Vector&); 145 | 146 | // arithmetic operators 147 | template 148 | Vector operator-(const Vector&); 149 | template 150 | Vector operator+(const Vector&, const Type&); 151 | template 152 | Vector operator+(const Type&, const Vector&); 153 | template 154 | Vector operator+(const Vector&, const Vector&); 155 | template 156 | Vector operator-(const Vector&, const Type&); 157 | template 158 | Vector operator-(const Type&, const Vector&); 159 | template 160 | Vector operator-(const Vector&, const Vector&); 161 | template 162 | Vector operator*(const Vector&, const Type&); 163 | template 164 | Vector operator*(const Type&, const Vector&); 165 | template 166 | Vector operator*(const Vector&, const Vector&); 167 | template 168 | Vector operator/(const Vector&, const Type&); 169 | template 170 | Vector operator/(const Type&, const Vector&); 171 | template 172 | Vector operator/(const Vector&, const Vector&); 173 | 174 | // dot product 175 | template 176 | Type dotProd(const Vector&, const Vector&); 177 | template complex 178 | dotProd(const Vector >&, const Vector >&); 179 | 180 | // utilities 181 | template Type sum(const Vector&); 182 | template Type min(const Vector&); 183 | template Type max(const Vector&); 184 | template Type norm(const Vector&); 185 | template Type norm(const Vector >&); 186 | template void swap(Vector&, Vector&); 187 | template Vector linspace(Type, Type, int); 188 | template Vector abs(const Vector >&); 189 | template Vector arg(const Vector >&); 190 | template Vector real(const Vector >&); 191 | template Vector imag(const Vector >&); 192 | template 193 | Vector > complexVector(const Vector&); 194 | template 195 | Vector > complexVector(const Vector&, 196 | const Vector&); 197 | 198 | 199 | #include "vector-impl.h" 200 | 201 | } 202 | // namespace splab 203 | 204 | 205 | #endif 206 | // VECTOR_H -------------------------------------------------------------------------------- /MatrixFunction/vector_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/vector_test.cpp -------------------------------------------------------------------------------- /MatrixFunction/vectormath-impl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * vectormath-impl.h 28 | * 29 | * Implementation for Vector math functions. 30 | * 31 | * Zhang Ming, 2010-08, Xi'an Jiaotong University. 32 | *****************************************************************************/ 33 | 34 | 35 | template 36 | Vector abs(const Vector &v) 37 | { 38 | Vector tmp(v.dim()); 39 | typename Vector::iterator itrL = tmp.begin(); 40 | typename Vector::const_iterator itrR = v.begin(); 41 | 42 | while (itrL != tmp.end()) 43 | *itrL++ = abs(*itrR++); 44 | 45 | return tmp; 46 | } 47 | 48 | 49 | template 50 | Vector cos(const Vector &v) 51 | { 52 | Vector tmp(v.dim()); 53 | typename Vector::iterator itrL = tmp.begin(); 54 | typename Vector::const_iterator itrR = v.begin(); 55 | 56 | while (itrL != tmp.end()) 57 | *itrL++ = cos(*itrR++); 58 | 59 | return tmp; 60 | } 61 | 62 | 63 | template 64 | Vector sin(const Vector &v) 65 | { 66 | Vector tmp(v.dim()); 67 | typename Vector::iterator itrL = tmp.begin(); 68 | typename Vector::const_iterator itrR = v.begin(); 69 | 70 | while (itrL != tmp.end()) 71 | *itrL++ = sin(*itrR++); 72 | 73 | return tmp; 74 | } 75 | 76 | 77 | template 78 | Vector tan(const Vector &v) 79 | { 80 | Vector tmp(v.dim()); 81 | typename Vector::iterator itrL = tmp.begin(); 82 | typename Vector::const_iterator itrR = v.begin(); 83 | 84 | while (itrL != tmp.end()) 85 | *itrL++ = tan(*itrR++); 86 | 87 | return tmp; 88 | } 89 | 90 | 91 | template 92 | Vector acos(const Vector &v) 93 | { 94 | Vector tmp(v.dim()); 95 | typename Vector::iterator itrL = tmp.begin(); 96 | typename Vector::const_iterator itrR = v.begin(); 97 | 98 | while (itrL != tmp.end()) 99 | *itrL++ = acos(*itrR++); 100 | 101 | return tmp; 102 | } 103 | 104 | 105 | template 106 | Vector asin(const Vector &v) 107 | { 108 | Vector tmp(v.dim()); 109 | typename Vector::iterator itrL = tmp.begin(); 110 | typename Vector::const_iterator itrR = v.begin(); 111 | 112 | while (itrL != tmp.end()) 113 | *itrL++ = asin(*itrR++); 114 | 115 | return tmp; 116 | } 117 | 118 | 119 | template 120 | Vector atan(const Vector &v) 121 | { 122 | Vector tmp(v.dim()); 123 | typename Vector::iterator itrL = tmp.begin(); 124 | typename Vector::const_iterator itrR = v.begin(); 125 | 126 | while (itrL != tmp.end()) 127 | *itrL++ = atan(*itrR++); 128 | 129 | return tmp; 130 | } 131 | 132 | 133 | template 134 | Vector exp(const Vector &v) 135 | { 136 | Vector tmp(v.dim()); 137 | typename Vector::iterator itrL = tmp.begin(); 138 | typename Vector::const_iterator itrR = v.begin(); 139 | 140 | while (itrL != tmp.end()) 141 | *itrL++ = exp(*itrR++); 142 | 143 | return tmp; 144 | } 145 | 146 | 147 | template 148 | Vector log(const Vector &v) 149 | { 150 | Vector tmp(v.dim()); 151 | typename Vector::iterator itrL = tmp.begin(); 152 | typename Vector::const_iterator itrR = v.begin(); 153 | 154 | while (itrL != tmp.end()) 155 | *itrL++ = log(*itrR++); 156 | 157 | return tmp; 158 | } 159 | 160 | 161 | template 162 | Vector log10(const Vector &v) 163 | { 164 | Vector tmp(v.dim()); 165 | typename Vector::iterator itrL = tmp.begin(); 166 | typename Vector::const_iterator itrR = v.begin(); 167 | 168 | while (itrL != tmp.end()) 169 | *itrL++ = log10(*itrR++); 170 | 171 | return tmp; 172 | } 173 | 174 | 175 | template 176 | Vector sqrt(const Vector &v) 177 | { 178 | Vector tmp(v.dim()); 179 | typename Vector::iterator itrL = tmp.begin(); 180 | typename Vector::const_iterator itrR = v.begin(); 181 | 182 | while (itrL != tmp.end()) 183 | *itrL++ = sqrt(*itrR++); 184 | 185 | return tmp; 186 | } 187 | 188 | 189 | template 190 | Vector pow(const Vector &b, const Vector &e) 191 | { 192 | assert(b.dim() == e.dim()); 193 | 194 | Vector tmp(b.dim()); 195 | typename Vector::iterator itrL = tmp.begin(); 196 | typename Vector::const_iterator itrR1 = b.begin(); 197 | typename Vector::const_iterator itrR2 = e.begin(); 198 | 199 | while (itrL != tmp.end()) 200 | *itrL++ = pow(*itrR1++, *itrR2++); 201 | 202 | return tmp; 203 | } 204 | 205 | 206 | template 207 | Vector pow(const Vector &b, const Type &e) 208 | { 209 | Vector tmp(b.dim()); 210 | typename Vector::iterator itrL = tmp.begin(); 211 | typename Vector::const_iterator itrR = b.begin(); 212 | 213 | while (itrL != tmp.end()) 214 | *itrL++ = pow(*itrR++, e); 215 | 216 | return tmp; 217 | } 218 | 219 | 220 | template 221 | Vector pow(const Type &b, const Vector &e) 222 | { 223 | Vector tmp(e.dim()); 224 | typename Vector::iterator itrL = tmp.begin(); 225 | typename Vector::const_iterator itrR = e.begin(); 226 | 227 | while (itrL != tmp.end()) 228 | *itrL++ = pow(b, *itrR++); 229 | 230 | return tmp; 231 | } 232 | 233 | 234 | /** 235 | * Normal distribution with expectation "u" and variance "r". 236 | */ 237 | template 238 | Vector gauss(const Vector &x, const Type &u, const Type &r) 239 | { 240 | Vector tmp(x); 241 | 242 | tmp = (tmp - u)*(tmp - u) / (-2 * r*r); 243 | tmp = exp(tmp) / Type((sqrt(2 * PI)*r)); 244 | 245 | return tmp; 246 | } -------------------------------------------------------------------------------- /MatrixFunction/vectormath.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2011 Zhang Ming (M. Zhang), zmjerry@163.com 3 | * 4 | * This program is free software; you can redistribute it and/or modify it 5 | * under the terms of the GNU General Public License as published by the 6 | * Free Software Foundation, either version 2 or any later version. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, 12 | * this list of conditions and the following disclaimer. 13 | * 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * This program is distributed in the hope that it will be useful, but WITHOUT 19 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 20 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 21 | * more details. A copy of the GNU General Public License is available at: 22 | * http://www.fsf.org/licensing/licenses 23 | */ 24 | 25 | 26 | /***************************************************************************** 27 | * vectormath.h 28 | * 29 | * This file provides the basic math functions such as: 30 | * cos sin tan acos asin atan 31 | * abs exp log log10 sqrt pow 32 | * 33 | * Zhang Ming, 2010-08, Xi'an Jiaotong University. 34 | *****************************************************************************/ 35 | 36 | 37 | #ifndef VECTORMATH_H 38 | #define VECTORMATH_H 39 | 40 | 41 | #include "vector.h" 42 | 43 | 44 | namespace splab 45 | { 46 | 47 | template Vector abs(const Vector&); 48 | template Vector cos(const Vector&); 49 | template Vector sin(const Vector&); 50 | template Vector tan(const Vector&); 51 | template Vector acos(const Vector&); 52 | template Vector asin(const Vector&); 53 | template Vector atan(const Vector&); 54 | 55 | template Vector exp(const Vector&); 56 | template Vector log(const Vector&); 57 | template Vector log10(const Vector&); 58 | 59 | template Vector sqrt(const Vector&); 60 | template Vector pow(const Vector&, 61 | const Vector&); 62 | template Vector pow(const Vector&, 63 | const Type&); 64 | template Vector pow(const Type&, 65 | const Vector&); 66 | template 67 | Vector gauss(const Vector&, const Type&, const Type&); 68 | 69 | 70 | #include "vectormath-impl.h" 71 | 72 | } 73 | // namespace splab 74 | 75 | 76 | #endif 77 | // VECTORMATH_H -------------------------------------------------------------------------------- /MatrixFunction/vectormath_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/renjiashuo/MatrixFunction/f55d73efd0e1441c94ecbabfb46bd09d9627c305/MatrixFunction/vectormath_test.cpp -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MatrixFunction 2 | 【非原创】【授权协议: MIT】C++矩阵计算相关函数源码。转载自http://my.oschina.net/zmjerry/blog 3 | 4 | 测试环境:visual studio 2013 5 | 6 | 一、C++向量类模板(支持实数向量与复数向量的各种运算) 7 | 8 | 头文件: 9 | usingdeclare.h、 10 | constants.h、 11 | vector.h。 12 | 实现文件: 13 | vector-impl.h。 14 | 测试代码: 15 | vector_test.cpp。 16 | 17 | 二、C++矩阵类模板(支持实数矩阵与复数矩阵的各种运算) 18 | 19 | 头文件: 20 | matrix.h。 21 | 实现文件: 22 | matrix-impl.h。 23 | 测试代码: 24 | matrix_test.cpp。 25 | 26 | 三、常用数学函数向量版本的C++实现 27 | 28 | 头文件: 29 | vectormath.h。 30 | 实现文件: 31 | vectormath-impl.h。 32 | 测试代码: 33 | vectormath_test.cpp。 34 | 35 | 四、常用数学函数矩阵版本的C++实现 36 | 37 | 头文件: 38 | matrixmath.h。 39 | 实现文件: 40 | matrixmath-impl.h。 41 | 测试代码: 42 | matrixmath_test.cpp。 43 | 44 | 五、实数矩阵Cholesky分解算法的C++实现 45 | 46 | 头文件: 47 | cholesky.h。 48 | 实现文件: 49 | cholesky-impl.h。 50 | 测试代码: 51 | cholesky_test.cpp。 52 | 53 | 六、复数矩阵Cholesky分解算法的C++实现 54 | 55 | 头文件: 56 | ccholesky.h。 57 | 实现文件: 58 | ccholesky-impl.h。 59 | 测试代码: 60 | ccholesky_test.cpp。 61 | 62 | 七、实数矩阵与复数矩阵LU分解算法的C++实现 63 | 64 | 头文件: 65 | lud.h。 66 | 实现文件: 67 | lud-impl.h。 68 | 测试代码: 69 | lud_test.cpp。 70 | 71 | 八、实数矩阵QR分解算法的C++实现 72 | 73 | 头文件: 74 | qrd.h。 75 | 实现文件: 76 | qrd-impl.h。 77 | 测试代码: 78 | qrd_test.cpp。 79 | 80 | 九、复数矩阵QR分解算法的C++实现 81 | 82 | 头文件: 83 | cqrd.h。 84 | 实现文件: 85 | cqrd-impl.h。 86 | 测试代码: 87 | cqrd_test.cpp。 88 | 89 | 十、实数矩阵SVD分解算法的C++实现 90 | 91 | 头文件: 92 | svd.h。 93 | 实现文件: 94 | svd-impl.h。 95 | 测试代码: 96 | svd_test.cpp。 97 | 98 | 十一、复数矩阵SVD分解算法的C++实现 99 | 100 | 头文件: 101 | csvd.h。 102 | 实现文件: 103 | csvd-impl.h。 104 | 测试代码: 105 | csvd_test.cpp。 106 | 107 | 十二、实数矩阵特征值分解算法C++实现 108 | 109 | 头文件: 110 | evd.h。 111 | 实现文件: 112 | evd-impl.h。 113 | 测试代码: 114 | evd_test.cpp。 115 | 116 | 十三、复数矩阵特征值分解算法的C++实现 117 | 118 | 头文件: 119 | cevd.h。 120 | 实现文件: 121 | cevd-impl.h。 122 | 测试代码: 123 | cevd_test.cpp。 124 | 125 | 十四、各种实数矩阵与复数矩阵求逆算法的C++实现 126 | 127 | 头文件: 128 | inverse.h。 129 | 实现文件: 130 | inverse-impl.h。 131 | 测试代码: 132 | inverse_test.cpp。 133 | 134 | 十五、实数矩阵与复数矩阵伪逆算法的C++实现 135 | 136 | 头文件: 137 | pseudoinverse.h。 138 | 实现文件: 139 | pseudoinverse-impl.h。 140 | 测试代码: 141 | pseudoinverse_test.cpp。 142 | 143 | 十六、各种线性方程组求解算法C++实现(支持复系数方程组) 144 | 145 | 头文件: 146 | linequs1.h。 147 | 实现文件: 148 | linequs1-impl.h。 149 | 测试代码: 150 | linequs1_test.cpp。 151 | 152 | 十七、最小二乘解与极小范数解算法的C++实现(支持复系数方程组) 153 | 154 | 头文件: 155 | linequs2.h。 156 | 实现文件: 157 | linequs2-impl.h。 158 | 测试代码: 159 | linequs2_test.cpp。 160 | 161 | 十八、病态方程组求解方法的C++实现(支持复系数方程组) 162 | 163 | 头文件: 164 | linequs3.h。 165 | 实现文件: 166 | linequs3-impl.h。 167 | 测试代码: 168 | linequs3_test.cpp。 169 | 170 | 十九、非线性方程求根算法的C++实现 171 | 172 | 头文件: 173 | nlfunc.h、 174 | nleroot.h。 175 | 实现文件: 176 | nleroot-impl.h。 177 | 测试代码: 178 | nleroot_test.cpp。 179 | 180 | 二十、非线性方程组求解算法的C++实现 181 | 182 | 头文件: 183 | nlfuncs.h、 184 | nleroots.h。 185 | 实现文件: 186 | nleroots-impl.h。 187 | 测试代码: 188 | nleroots_test.cpp。 189 | 190 | 二十一、Romberg数值积分算法的C++实现 191 | 192 | 头文件: 193 | integrand.h、 194 | integral.h。 195 | 实现文件: 196 | integral-impl.h。 197 | 测试代码: 198 | integral_test.cpp。 199 | 200 | 二十二、Newton插值算法的C++实现 201 | 202 | 头文件: 203 | interpolation.h、 204 | newtoninterp.h。 205 | 实现文件: 206 | newtoninterp-impl.h。 207 | 测试代码: 208 | newtoninterp_test.cpp。 209 | 210 | 二十三、三次样条插值算法的C++实现 211 | 212 | 头文件: 213 | spline3interp.h。 214 | 实现文件: 215 | spline3interp-impl.h。 216 | 测试代码: 217 | spline3interp_test.cpp。 218 | 219 | 二十四、最小二乘曲线拟合算法的C++实现 220 | 221 | 头文件: 222 | fitcurves.h、 223 | lsfitting.h。 224 | 实现文件: 225 | lsfitting-impl.h。 226 | 测试代码: 227 | lsfit_test.cpp。 228 | 229 | 二十五、最速下降优化算法的C++实现 230 | 231 | 头文件: 232 | objfunc.h、 233 | linesearch.h、 234 | steepdesc.h。 235 | 实现文件: 236 | linesearch-impl.h、 237 | steepdesc-impl.h。 238 | 测试代码: 239 | steepdesc_test.cpp。 240 | 241 | 二十六、共轭梯度优化算法的C++实现 242 | 243 | 头文件: 244 | conjgrad.h。 245 | 实现文件: 246 | conjgrad-impl.h。 247 | 测试代码: 248 | conjgrad_test.cpp。 249 | 250 | 二十七、信号处理算法中常用辅助函数的C++实现 251 | 252 | 头文件: 253 | utilities.h。 254 | 实现文件: 255 | utilities-impl.h。 256 | 测试代码: 257 | utilities_test.cpp。 258 | 259 | 二十八、类似Matlab中FFT函数调用形式的C++实现 260 | 261 | 头文件: 262 | fft.h。 263 | 实现文件: 264 | fft-impl.h。 265 | 测试代码: 266 | fft_test.cpp。 267 | 268 | 二十九、2的整次幂长度FFT算法的C++实现 269 | 270 | 头文件: 271 | fftmr.h。 272 | 实现文件: 273 | fftmr-impl.h。 274 | 测试代码: 275 | fftmr_test.cpp。 276 | 277 | 三十、任意长度FFT算法的C++实现 278 | 279 | 头文件: 280 | fftpf.h。 281 | 实现文件: 282 | fftpf-impl.h。 283 | 测试代码: 284 | fftpf_test.cpp。 285 | 286 | 三十一、BFGS优化算法的C++实现 287 | 288 | 头文件: 289 | bfgs.h。 290 | 实现文件: 291 | bfgs-impl.h。 292 | 测试代码: 293 | bfgs_test.cpp。 294 | --------------------------------------------------------------------------------