How to reduce matrix into row echelon form in Python
$begingroup$
I'm working on a linear algebra homework for a data science class. I'm suppose to make this matrix into row echelon form but I'm stuck.
Here's the current output
I would like to get rid of -0.75, 0.777, and 1.333 in A[2,0], A[3,0], and A[3,1] respectively; they should be zeroed out.
Below is my current code... can anybody please nudge me in the right direction and tell me what step I'm missing?
import numpy as np
def fixRowTwo(A) :
# Sets the sub-diagonal elements of row two to zero
A[2] = A[2] - A[2,0] * A[1]
A[2] = A[2] - A[2,1] * A[1]
# Test if diagonal element is not zero.
if A[2,2] == 0 :
# Add a lower row to row two.
A[2] = A[2] + A[3]
# Sets the sub-diagonal elements to zero again ???
A[2] = A[2] - A[2,0] * A[1]
A[2] = A[2] - A[2,1] * A[1]
if A[2,2] == 0 :
print("S I N G U L A R")
sys.Exit()
# Set the diagonal element to one
A[2] = A[2] / A[2,2]
return A
def fixRowThree(A) :
# Sets the sub-diagonal elements of row two to zero
A[3] = A[3] - A[3,0] * A[2]
A[3] = A[3] - A[3,1] * A[2]
A[3] = A[3] - A[3,2] * A[2]
# Test if diagonal element is not zero.
if A[3,3] == 0:
print("S I N G U L A R")
sys.Exit()
# Set the diagonal element to one
A[3] = A[3] / A[3,3]
return A
A = np.array([
[1, 7, 4, 3],
[0, 1, 2, 3],
[3, 2, 0, 3],
[1, 3, 1, 3]
], dtype=np.float_)
fixRowTwo(A)
print("")
print("Row Two:")
print(A)
fixRowThree(A)
print("")
print("Row Three:")
print(A)
linear-algebra python
$endgroup$
add a comment |
$begingroup$
I'm working on a linear algebra homework for a data science class. I'm suppose to make this matrix into row echelon form but I'm stuck.
Here's the current output
I would like to get rid of -0.75, 0.777, and 1.333 in A[2,0], A[3,0], and A[3,1] respectively; they should be zeroed out.
Below is my current code... can anybody please nudge me in the right direction and tell me what step I'm missing?
import numpy as np
def fixRowTwo(A) :
# Sets the sub-diagonal elements of row two to zero
A[2] = A[2] - A[2,0] * A[1]
A[2] = A[2] - A[2,1] * A[1]
# Test if diagonal element is not zero.
if A[2,2] == 0 :
# Add a lower row to row two.
A[2] = A[2] + A[3]
# Sets the sub-diagonal elements to zero again ???
A[2] = A[2] - A[2,0] * A[1]
A[2] = A[2] - A[2,1] * A[1]
if A[2,2] == 0 :
print("S I N G U L A R")
sys.Exit()
# Set the diagonal element to one
A[2] = A[2] / A[2,2]
return A
def fixRowThree(A) :
# Sets the sub-diagonal elements of row two to zero
A[3] = A[3] - A[3,0] * A[2]
A[3] = A[3] - A[3,1] * A[2]
A[3] = A[3] - A[3,2] * A[2]
# Test if diagonal element is not zero.
if A[3,3] == 0:
print("S I N G U L A R")
sys.Exit()
# Set the diagonal element to one
A[3] = A[3] / A[3,3]
return A
A = np.array([
[1, 7, 4, 3],
[0, 1, 2, 3],
[3, 2, 0, 3],
[1, 3, 1, 3]
], dtype=np.float_)
fixRowTwo(A)
print("")
print("Row Two:")
print(A)
fixRowThree(A)
print("")
print("Row Three:")
print(A)
linear-algebra python
$endgroup$
add a comment |
$begingroup$
I'm working on a linear algebra homework for a data science class. I'm suppose to make this matrix into row echelon form but I'm stuck.
Here's the current output
I would like to get rid of -0.75, 0.777, and 1.333 in A[2,0], A[3,0], and A[3,1] respectively; they should be zeroed out.
Below is my current code... can anybody please nudge me in the right direction and tell me what step I'm missing?
import numpy as np
def fixRowTwo(A) :
# Sets the sub-diagonal elements of row two to zero
A[2] = A[2] - A[2,0] * A[1]
A[2] = A[2] - A[2,1] * A[1]
# Test if diagonal element is not zero.
if A[2,2] == 0 :
# Add a lower row to row two.
A[2] = A[2] + A[3]
# Sets the sub-diagonal elements to zero again ???
A[2] = A[2] - A[2,0] * A[1]
A[2] = A[2] - A[2,1] * A[1]
if A[2,2] == 0 :
print("S I N G U L A R")
sys.Exit()
# Set the diagonal element to one
A[2] = A[2] / A[2,2]
return A
def fixRowThree(A) :
# Sets the sub-diagonal elements of row two to zero
A[3] = A[3] - A[3,0] * A[2]
A[3] = A[3] - A[3,1] * A[2]
A[3] = A[3] - A[3,2] * A[2]
# Test if diagonal element is not zero.
if A[3,3] == 0:
print("S I N G U L A R")
sys.Exit()
# Set the diagonal element to one
A[3] = A[3] / A[3,3]
return A
A = np.array([
[1, 7, 4, 3],
[0, 1, 2, 3],
[3, 2, 0, 3],
[1, 3, 1, 3]
], dtype=np.float_)
fixRowTwo(A)
print("")
print("Row Two:")
print(A)
fixRowThree(A)
print("")
print("Row Three:")
print(A)
linear-algebra python
$endgroup$
I'm working on a linear algebra homework for a data science class. I'm suppose to make this matrix into row echelon form but I'm stuck.
Here's the current output
I would like to get rid of -0.75, 0.777, and 1.333 in A[2,0], A[3,0], and A[3,1] respectively; they should be zeroed out.
Below is my current code... can anybody please nudge me in the right direction and tell me what step I'm missing?
import numpy as np
def fixRowTwo(A) :
# Sets the sub-diagonal elements of row two to zero
A[2] = A[2] - A[2,0] * A[1]
A[2] = A[2] - A[2,1] * A[1]
# Test if diagonal element is not zero.
if A[2,2] == 0 :
# Add a lower row to row two.
A[2] = A[2] + A[3]
# Sets the sub-diagonal elements to zero again ???
A[2] = A[2] - A[2,0] * A[1]
A[2] = A[2] - A[2,1] * A[1]
if A[2,2] == 0 :
print("S I N G U L A R")
sys.Exit()
# Set the diagonal element to one
A[2] = A[2] / A[2,2]
return A
def fixRowThree(A) :
# Sets the sub-diagonal elements of row two to zero
A[3] = A[3] - A[3,0] * A[2]
A[3] = A[3] - A[3,1] * A[2]
A[3] = A[3] - A[3,2] * A[2]
# Test if diagonal element is not zero.
if A[3,3] == 0:
print("S I N G U L A R")
sys.Exit()
# Set the diagonal element to one
A[3] = A[3] / A[3,3]
return A
A = np.array([
[1, 7, 4, 3],
[0, 1, 2, 3],
[3, 2, 0, 3],
[1, 3, 1, 3]
], dtype=np.float_)
fixRowTwo(A)
print("")
print("Row Two:")
print(A)
fixRowThree(A)
print("")
print("Row Three:")
print(A)
linear-algebra python
linear-algebra python
asked Jan 14 at 10:37
DdflowwDdfloww
32
32
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
Since the Gaussian process is recursive, we utilize it in the code.
import numpy as np
def row_echelon(A):
""" Return Row Echelon Form of matrix A """
# if matrix A has no columns or rows,
# it is already in REF, so we return itself
r, c = A.shape
if r == 0 or c == 0:
return A
# we search for non-zero element in the first column
for i in range(len(A)):
if A[i,0] != 0:
break
else:
# if all elements in the first column is zero,
# we perform REF on matrix from second column
B = row_echelon(A[:,1:])
# and then add the first zero-column back
return np.hstack([A[:,:1], B])
# if non-zero element happens not in the first row,
# we switch rows
if i > 0:
ith_row = A[i].copy()
A[i] = A[0]
A[0] = ith_row
# we divide first row by first element in it
A[0] = A[0] / A[0,0]
# we subtract all subsequent rows with first row (it has 1 now as first element)
# multiplied by the corresponding element in the first column
A[1:] -= A[0] * A[1:,0:1]
# we perform REF on matrix from second row, from second column
B = row_echelon(A[1:,1:])
# we add first row and first (zero) column, and return
return np.vstack([A[:1], np.hstack([A[1:,:1], B]) ])
A = np.array([[4, 7, 3, 8],
[8, 3, 8, 7],
[2, 9, 5, 3]], dtype='float')
row_echelon(A)
Feel free to add something like print(A[1:,:1])
if you don't understand some of the constructions
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "69"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3073083%2fhow-to-reduce-matrix-into-row-echelon-form-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Since the Gaussian process is recursive, we utilize it in the code.
import numpy as np
def row_echelon(A):
""" Return Row Echelon Form of matrix A """
# if matrix A has no columns or rows,
# it is already in REF, so we return itself
r, c = A.shape
if r == 0 or c == 0:
return A
# we search for non-zero element in the first column
for i in range(len(A)):
if A[i,0] != 0:
break
else:
# if all elements in the first column is zero,
# we perform REF on matrix from second column
B = row_echelon(A[:,1:])
# and then add the first zero-column back
return np.hstack([A[:,:1], B])
# if non-zero element happens not in the first row,
# we switch rows
if i > 0:
ith_row = A[i].copy()
A[i] = A[0]
A[0] = ith_row
# we divide first row by first element in it
A[0] = A[0] / A[0,0]
# we subtract all subsequent rows with first row (it has 1 now as first element)
# multiplied by the corresponding element in the first column
A[1:] -= A[0] * A[1:,0:1]
# we perform REF on matrix from second row, from second column
B = row_echelon(A[1:,1:])
# we add first row and first (zero) column, and return
return np.vstack([A[:1], np.hstack([A[1:,:1], B]) ])
A = np.array([[4, 7, 3, 8],
[8, 3, 8, 7],
[2, 9, 5, 3]], dtype='float')
row_echelon(A)
Feel free to add something like print(A[1:,:1])
if you don't understand some of the constructions
$endgroup$
add a comment |
$begingroup$
Since the Gaussian process is recursive, we utilize it in the code.
import numpy as np
def row_echelon(A):
""" Return Row Echelon Form of matrix A """
# if matrix A has no columns or rows,
# it is already in REF, so we return itself
r, c = A.shape
if r == 0 or c == 0:
return A
# we search for non-zero element in the first column
for i in range(len(A)):
if A[i,0] != 0:
break
else:
# if all elements in the first column is zero,
# we perform REF on matrix from second column
B = row_echelon(A[:,1:])
# and then add the first zero-column back
return np.hstack([A[:,:1], B])
# if non-zero element happens not in the first row,
# we switch rows
if i > 0:
ith_row = A[i].copy()
A[i] = A[0]
A[0] = ith_row
# we divide first row by first element in it
A[0] = A[0] / A[0,0]
# we subtract all subsequent rows with first row (it has 1 now as first element)
# multiplied by the corresponding element in the first column
A[1:] -= A[0] * A[1:,0:1]
# we perform REF on matrix from second row, from second column
B = row_echelon(A[1:,1:])
# we add first row and first (zero) column, and return
return np.vstack([A[:1], np.hstack([A[1:,:1], B]) ])
A = np.array([[4, 7, 3, 8],
[8, 3, 8, 7],
[2, 9, 5, 3]], dtype='float')
row_echelon(A)
Feel free to add something like print(A[1:,:1])
if you don't understand some of the constructions
$endgroup$
add a comment |
$begingroup$
Since the Gaussian process is recursive, we utilize it in the code.
import numpy as np
def row_echelon(A):
""" Return Row Echelon Form of matrix A """
# if matrix A has no columns or rows,
# it is already in REF, so we return itself
r, c = A.shape
if r == 0 or c == 0:
return A
# we search for non-zero element in the first column
for i in range(len(A)):
if A[i,0] != 0:
break
else:
# if all elements in the first column is zero,
# we perform REF on matrix from second column
B = row_echelon(A[:,1:])
# and then add the first zero-column back
return np.hstack([A[:,:1], B])
# if non-zero element happens not in the first row,
# we switch rows
if i > 0:
ith_row = A[i].copy()
A[i] = A[0]
A[0] = ith_row
# we divide first row by first element in it
A[0] = A[0] / A[0,0]
# we subtract all subsequent rows with first row (it has 1 now as first element)
# multiplied by the corresponding element in the first column
A[1:] -= A[0] * A[1:,0:1]
# we perform REF on matrix from second row, from second column
B = row_echelon(A[1:,1:])
# we add first row and first (zero) column, and return
return np.vstack([A[:1], np.hstack([A[1:,:1], B]) ])
A = np.array([[4, 7, 3, 8],
[8, 3, 8, 7],
[2, 9, 5, 3]], dtype='float')
row_echelon(A)
Feel free to add something like print(A[1:,:1])
if you don't understand some of the constructions
$endgroup$
Since the Gaussian process is recursive, we utilize it in the code.
import numpy as np
def row_echelon(A):
""" Return Row Echelon Form of matrix A """
# if matrix A has no columns or rows,
# it is already in REF, so we return itself
r, c = A.shape
if r == 0 or c == 0:
return A
# we search for non-zero element in the first column
for i in range(len(A)):
if A[i,0] != 0:
break
else:
# if all elements in the first column is zero,
# we perform REF on matrix from second column
B = row_echelon(A[:,1:])
# and then add the first zero-column back
return np.hstack([A[:,:1], B])
# if non-zero element happens not in the first row,
# we switch rows
if i > 0:
ith_row = A[i].copy()
A[i] = A[0]
A[0] = ith_row
# we divide first row by first element in it
A[0] = A[0] / A[0,0]
# we subtract all subsequent rows with first row (it has 1 now as first element)
# multiplied by the corresponding element in the first column
A[1:] -= A[0] * A[1:,0:1]
# we perform REF on matrix from second row, from second column
B = row_echelon(A[1:,1:])
# we add first row and first (zero) column, and return
return np.vstack([A[:1], np.hstack([A[1:,:1], B]) ])
A = np.array([[4, 7, 3, 8],
[8, 3, 8, 7],
[2, 9, 5, 3]], dtype='float')
row_echelon(A)
Feel free to add something like print(A[1:,:1])
if you don't understand some of the constructions
answered Jan 14 at 11:19
Vasily MitchVasily Mitch
2,3241311
2,3241311
add a comment |
add a comment |
Thanks for contributing an answer to Mathematics Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3073083%2fhow-to-reduce-matrix-into-row-echelon-form-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown