Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
nmm_2020_site
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Erik Strand
nmm_2020_site
Commits
1f02bb00
Commit
1f02bb00
authored
Apr 9, 2020
by
Erik Strand
Browse files
Options
Downloads
Patches
Plain Diff
Use Eigen's SVD to solve linear least squares
parent
060a9be6
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
_code/CMakeLists.txt
+1
-0
1 addition, 0 deletions
_code/CMakeLists.txt
_code/pset_07/cpp/CMakeLists.txt
+19
-0
19 additions, 0 deletions
_code/pset_07/cpp/CMakeLists.txt
_code/pset_07/cpp/svd.cpp
+61
-0
61 additions, 0 deletions
_code/pset_07/cpp/svd.cpp
with
81 additions
and
0 deletions
_code/CMakeLists.txt
+
1
−
0
View file @
1f02bb00
...
...
@@ -16,3 +16,4 @@ add_subdirectory(notes)
add_subdirectory
(
pset_03
)
add_subdirectory
(
pset_04
)
add_subdirectory
(
pset_05/cpp
)
add_subdirectory
(
pset_07/cpp
)
This diff is collapsed.
Click to expand it.
_code/pset_07/cpp/CMakeLists.txt
0 → 100644
+
19
−
0
View file @
1f02bb00
#add_library(sim
# glut_grapher.cpp
# glut_grapher.h
#)
#target_link_libraries(sim PUBLIC common ${OPENGL_LIBRARIES} ${GLUT_LIBRARY})
#target_include_directories(sim PUBLIC
# ${OPENGL_INCLUDE_DIRS}
# ${GLUT_INCLUDE_DIRS}
#)
add_executable
(
svd
svd.cpp
)
target_link_libraries
(
svd shared_settings shared_code
)
#add_executable(diffusion
# diffusion.cpp
#)
#target_link_libraries(diffusion sim)
This diff is collapsed.
Click to expand it.
_code/pset_07/cpp/svd.cpp
0 → 100644
+
61
−
0
View file @
1f02bb00
#include
"xorshift.h"
#include
<Eigen/Dense>
#include
<iostream>
#include
<random>
#include
<vector>
using
namespace
std
;
using
namespace
Eigen
;
//--------------------------------------------------------------------------------------------------
XorShift64
rg1
;
std
::
random_device
rd
{};
std
::
mt19937
rg2
{
rd
()};
//--------------------------------------------------------------------------------------------------
Eigen
::
ArrayXd
draw_n_uniform
(
uint32_t
n
)
{
Eigen
::
ArrayXd
result
(
n
);
for
(
uint32_t
i
=
0
;
i
<
n
;
++
i
)
{
result
[
i
]
=
rg1
.
draw_double
();
}
return
result
;
}
//--------------------------------------------------------------------------------------------------
// TODO Box-Muller transform my uniform samples instead of using STL
Eigen
::
ArrayXd
draw_n_normal
(
uint32_t
n
,
double
std_deviation
)
{
std
::
normal_distribution
<>
normal
{
0
,
std_deviation
};
Eigen
::
ArrayXd
result
(
n
);
for
(
uint32_t
i
=
0
;
i
<
n
;
++
i
)
{
result
[
i
]
=
normal
(
rg2
);
}
return
result
;
}
//--------------------------------------------------------------------------------------------------
int
main
()
{
uint32_t
constexpr
n_warmup
=
1000
;
for
(
uint32_t
i
=
0
;
i
<
n_warmup
;
++
i
)
{
rg1
.
advance
();
}
uint32_t
constexpr
n_samples
=
100
;
double
const
std_deviation
=
0.5
;
Eigen
::
ArrayXd
samples
=
draw_n_uniform
(
n_samples
);
Eigen
::
ArrayXd
errors
=
draw_n_normal
(
n_samples
,
std_deviation
);
Eigen
::
ArrayXd
values
=
2
+
3
*
samples
+
errors
;
Eigen
::
MatrixXd
mat
(
n_samples
,
2
);
for
(
uint32_t
i
=
0
;
i
<
n_samples
;
++
i
)
{
mat
(
i
,
0
)
=
samples
[
i
];
mat
(
i
,
1
)
=
1
;
}
// Need at least ThinU and ThinV to use mat_svd.solve.
JacobiSVD
<
MatrixXd
>
mat_svd
(
mat
,
ComputeThinU
|
ComputeThinV
);
std
::
cout
<<
"singular values:
\n
"
<<
mat_svd
.
singularValues
()
<<
'\n'
;
//cout << "left singular vectors:\n" << mat_svd.matrixU() << '\n';
//cout << "right singular vectors:\n" << mat_svd.matrixV() << '\n';
Eigen
::
VectorXd
solution
=
mat_svd
.
solve
(
values
.
matrix
());
std
::
cout
<<
"solution:
\n
"
<<
solution
<<
'\n'
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment