Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
NumCSE
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Joost Opschoor
NumCSE
Commits
3a396d80
Commit
3a396d80
authored
Sep 16, 2019
by
Tandri Gauksson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
function arguments changed to const reference, and copying is explicit
parent
3f47a2b2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
32 deletions
+24
-32
Assignments/Codes/Introduction/MatrixBlocks/CodeExpert/solution/MatrixBlocks.hpp
...duction/MatrixBlocks/CodeExpert/solution/MatrixBlocks.hpp
+24
-32
No files found.
Assignments/Codes/Introduction/MatrixBlocks/CodeExpert/solution/MatrixBlocks.hpp
View file @
3a396d80
...
...
@@ -16,46 +16,36 @@ using namespace Eigen;
// END
/* SAM_LISTING_BEGIN_0 */
MatrixXd
zero_row_col
(
MatrixXd
A
,
int
p
,
int
q
)
{
MatrixXd
zero_row_col
(
const
MatrixXd
&
A
,
int
p
,
int
q
)
{
/*
* This function takes a matrix A, and returns a matrix that is exactly the
* same, except that row p and column q are set to zero.
*/
// We can use .rows() and .cols() to get the number of rows and columns in A.
int
rows
=
A
.
rows
();
int
cols
=
A
.
cols
();
VectorXd
v
;
VectorXd
u
;
// TO DO: Initialize u and v with zeros, and make sure they are of the
// appropriate sizes. START
u
=
VectorXd
::
Zero
(
cols
);
v
=
VectorXd
::
Zero
(
rows
);
// Make a copy of A.
MatrixXd
Anew
(
A
);
// TO DO: Set the entries of row number p and column number q to zero.
// Hint: We can access rows and columns of A by A.row() and A.col().
// The setZero() is useful here. START
Anew
.
row
(
p
).
setZero
();
Anew
.
col
(
q
).
setZero
();
// END
// We can access rows and columns of A by A.row() and A.col().
A
.
row
(
p
)
=
u
;
A
.
col
(
q
)
=
v
;
// Question: Have we now changed the matrix that was passed as an argument to
// this function, or is A a copy of that matrix?
return
A
;
return
Anew
;
}
/* SAM_LISTING_END_0 */
/* SAM_LISTING_BEGIN_1 */
MatrixXd
swap_left_right_blocks
(
MatrixXd
A
,
int
p
)
{
MatrixXd
swap_left_right_blocks
(
const
MatrixXd
&
A
,
int
p
)
{
/*
* Writing as a block matrix A = [B C], where B denotes the first p columns of
* A, and C denotes the q=(A.cols() - p) last columns, this functions returns
* D = [C B].
*/
MatrixXd
B
,
C
;
// We can use .rows() and .cols() to get the number of rows and columns in A.
int
q
=
A
.
cols
()
-
p
;
// A.block( i, j, m, n ) returns the m by n block that has its top-left corner
...
...
@@ -67,31 +57,33 @@ MatrixXd swap_left_right_blocks(MatrixXd A, int p) {
// columns of A. START
C
=
A
.
block
(
0
,
p
,
A
.
rows
(),
q
);
// END
// Make a copy of A.
MatrixXd
Anew
(
A
);
// The block() method can access arbitrary blocks within a matrix.
// For our purposes, it is actually simpler to use leftCols() and rightCols().
A
.
leftCols
(
q
)
=
C
;
A
new
.
leftCols
(
q
)
=
C
;
// TO DO: Use A.rightCols() to fill in the remaining columns of the new matrix
// A. START
A
.
rightCols
(
p
)
=
B
;
A
new
.
rightCols
(
p
)
=
B
;
// END
// Tip: Many more methods exist that are special cases of block(),
// e.g. topRows(), bottomRows(), topLeftCorner(), bottomLeftCorner(), ...
// For vectors we have head(), tail(), and segment().
return
A
;
return
A
new
;
}
/* SAM_LISTING_END_1 */
/* SAM_LISTING_BEGIN_
3
*/
/* SAM_LISTING_BEGIN_
2
*/
MatrixXd
tridiagonal
(
int
n
,
double
a
,
double
b
,
double
c
)
{
/*
* This function creates an n by n tridiagonal matrix with the values
* a on the subdiagonal,
* a on the
first
subdiagonal,
* b on the diagonal, and
* c on the superdiagonal.
* c on the
first
superdiagonal.
* Example for n=5:
* [ b c 0 0 0 ]
* [ a b c 0 0 ]
...
...
@@ -113,6 +105,6 @@ MatrixXd tridiagonal(int n, double a, double b, double c) {
// END
return
A
;
}
/* SAM_LISTING_END_
3
*/
/* SAM_LISTING_END_
2
*/
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment