Python: Missing Digit When Printing Out Column
up vote
0
down vote
favorite
I am confused as to why my digits are not printing correctly from my Matrix column.
It is missing the trailing digits after the first digit. However, it performs the correct operations. I am not sure as to why the output is like this and have looked over my code a few times. I may not be catching something.
I would like your help in finding my logic error.
class Matrix:
def __init__(self, Rowsp=[]):
"""Matrix Constructor"""
self.Rowsp = Rowsp
self.Colsp = [[self.Rowsp[j][i] for j in range(len(self.Rowsp))] for i in range(len(self.Rowsp[0]))]
def copy(self):
"""
Returns a copy of the matrix for calculations
"""
copy =
rows = len(self.Rowsp)
cols = len(self.Rowsp[0])
for i in range(rows):
copy.append(list())
for j in range(cols):
copy[i].append(self.Rowsp[i][j])
return Matrix(copy)
Setters
def setCol(self, j, u):
"""
changes the j-th column to be the list u.
If u is not the same length as the existing columns,
then the constructor should raise a ValueError
with the message Incompatible column length.
"""
rows = len(self.Rowsp)
if (len(u) != rows):
#raise ValueError - SWAP OUT FOR VAL ERROR
print("ERROR: Incompatible column length")
else:
j -= 1
for i in range(rows):
self.Rowsp[i][j] = u[i]
def setRow(self, i, v):
"""
changes the i-th row to be the list v.
If v is not the same length as the existing rows,
then the constructor should raise a ValueError
with the message Incompatible row length.
"""
if len(self.Rowsp[0]) != len(v):
#raise ValueError - SWAP OUT FOR VAL ERROR
print("ERROR: Incompatible row length")
else:
self.Rowsp[i - 1] = v
def setEntry(self, i, j, a):
"""
changes the existing a_ij entry in the matrix to a
"""
self.Rowsp[i - 1][j - 1] = a
Getters
def getCol(self, j):
"""
returns the j-th column as a list.
"""
return self.Colsp[j - 1]
def getRow(self, i):
"""
returns the i-th row as a list v.
"""
return self.Rowsp[i - 1]
def getEntry(self, i, j):
"""
returns the existing a_ij entry in the matrix.
"""
return self.Rowsp[i - 1][j - 1]
def getColSpace(self):
"""
returns the list of vectors that make up the column space of the matrix object
"""
c = self.Colsp.copy()
return c
def getRowSpace(self):
"""
returns the list of vectors that make up the row space of the matrix object
"""
r = self.Rowsp.copy()
return r
Test Case
print("The 2nd row is:", A.getRow(2))
print("The 3rd column is:", A.getCol(3))
print()
A.setRow(2, [40, 50])
A.setCol(2, [30, 4, 1])
print(A)
My result:
The 2nd row is: [40, 50, 60]
The 3rd column is: [3, 6]
ERROR: Incompatible row length
ERROR: Incompatible column length
10 20 30
40 50 60
What I should get:
The 2nd row is: [40, 50, 60]
The 3rd column is: [30, 60]
ERROR: Incompatible row length.
ERROR: Incompatible column space.
10 20 30
40 50 60
python matrix logic digits
add a comment |
up vote
0
down vote
favorite
I am confused as to why my digits are not printing correctly from my Matrix column.
It is missing the trailing digits after the first digit. However, it performs the correct operations. I am not sure as to why the output is like this and have looked over my code a few times. I may not be catching something.
I would like your help in finding my logic error.
class Matrix:
def __init__(self, Rowsp=[]):
"""Matrix Constructor"""
self.Rowsp = Rowsp
self.Colsp = [[self.Rowsp[j][i] for j in range(len(self.Rowsp))] for i in range(len(self.Rowsp[0]))]
def copy(self):
"""
Returns a copy of the matrix for calculations
"""
copy =
rows = len(self.Rowsp)
cols = len(self.Rowsp[0])
for i in range(rows):
copy.append(list())
for j in range(cols):
copy[i].append(self.Rowsp[i][j])
return Matrix(copy)
Setters
def setCol(self, j, u):
"""
changes the j-th column to be the list u.
If u is not the same length as the existing columns,
then the constructor should raise a ValueError
with the message Incompatible column length.
"""
rows = len(self.Rowsp)
if (len(u) != rows):
#raise ValueError - SWAP OUT FOR VAL ERROR
print("ERROR: Incompatible column length")
else:
j -= 1
for i in range(rows):
self.Rowsp[i][j] = u[i]
def setRow(self, i, v):
"""
changes the i-th row to be the list v.
If v is not the same length as the existing rows,
then the constructor should raise a ValueError
with the message Incompatible row length.
"""
if len(self.Rowsp[0]) != len(v):
#raise ValueError - SWAP OUT FOR VAL ERROR
print("ERROR: Incompatible row length")
else:
self.Rowsp[i - 1] = v
def setEntry(self, i, j, a):
"""
changes the existing a_ij entry in the matrix to a
"""
self.Rowsp[i - 1][j - 1] = a
Getters
def getCol(self, j):
"""
returns the j-th column as a list.
"""
return self.Colsp[j - 1]
def getRow(self, i):
"""
returns the i-th row as a list v.
"""
return self.Rowsp[i - 1]
def getEntry(self, i, j):
"""
returns the existing a_ij entry in the matrix.
"""
return self.Rowsp[i - 1][j - 1]
def getColSpace(self):
"""
returns the list of vectors that make up the column space of the matrix object
"""
c = self.Colsp.copy()
return c
def getRowSpace(self):
"""
returns the list of vectors that make up the row space of the matrix object
"""
r = self.Rowsp.copy()
return r
Test Case
print("The 2nd row is:", A.getRow(2))
print("The 3rd column is:", A.getCol(3))
print()
A.setRow(2, [40, 50])
A.setCol(2, [30, 4, 1])
print(A)
My result:
The 2nd row is: [40, 50, 60]
The 3rd column is: [3, 6]
ERROR: Incompatible row length
ERROR: Incompatible column length
10 20 30
40 50 60
What I should get:
The 2nd row is: [40, 50, 60]
The 3rd column is: [30, 60]
ERROR: Incompatible row length.
ERROR: Incompatible column space.
10 20 30
40 50 60
python matrix logic digits
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am confused as to why my digits are not printing correctly from my Matrix column.
It is missing the trailing digits after the first digit. However, it performs the correct operations. I am not sure as to why the output is like this and have looked over my code a few times. I may not be catching something.
I would like your help in finding my logic error.
class Matrix:
def __init__(self, Rowsp=[]):
"""Matrix Constructor"""
self.Rowsp = Rowsp
self.Colsp = [[self.Rowsp[j][i] for j in range(len(self.Rowsp))] for i in range(len(self.Rowsp[0]))]
def copy(self):
"""
Returns a copy of the matrix for calculations
"""
copy =
rows = len(self.Rowsp)
cols = len(self.Rowsp[0])
for i in range(rows):
copy.append(list())
for j in range(cols):
copy[i].append(self.Rowsp[i][j])
return Matrix(copy)
Setters
def setCol(self, j, u):
"""
changes the j-th column to be the list u.
If u is not the same length as the existing columns,
then the constructor should raise a ValueError
with the message Incompatible column length.
"""
rows = len(self.Rowsp)
if (len(u) != rows):
#raise ValueError - SWAP OUT FOR VAL ERROR
print("ERROR: Incompatible column length")
else:
j -= 1
for i in range(rows):
self.Rowsp[i][j] = u[i]
def setRow(self, i, v):
"""
changes the i-th row to be the list v.
If v is not the same length as the existing rows,
then the constructor should raise a ValueError
with the message Incompatible row length.
"""
if len(self.Rowsp[0]) != len(v):
#raise ValueError - SWAP OUT FOR VAL ERROR
print("ERROR: Incompatible row length")
else:
self.Rowsp[i - 1] = v
def setEntry(self, i, j, a):
"""
changes the existing a_ij entry in the matrix to a
"""
self.Rowsp[i - 1][j - 1] = a
Getters
def getCol(self, j):
"""
returns the j-th column as a list.
"""
return self.Colsp[j - 1]
def getRow(self, i):
"""
returns the i-th row as a list v.
"""
return self.Rowsp[i - 1]
def getEntry(self, i, j):
"""
returns the existing a_ij entry in the matrix.
"""
return self.Rowsp[i - 1][j - 1]
def getColSpace(self):
"""
returns the list of vectors that make up the column space of the matrix object
"""
c = self.Colsp.copy()
return c
def getRowSpace(self):
"""
returns the list of vectors that make up the row space of the matrix object
"""
r = self.Rowsp.copy()
return r
Test Case
print("The 2nd row is:", A.getRow(2))
print("The 3rd column is:", A.getCol(3))
print()
A.setRow(2, [40, 50])
A.setCol(2, [30, 4, 1])
print(A)
My result:
The 2nd row is: [40, 50, 60]
The 3rd column is: [3, 6]
ERROR: Incompatible row length
ERROR: Incompatible column length
10 20 30
40 50 60
What I should get:
The 2nd row is: [40, 50, 60]
The 3rd column is: [30, 60]
ERROR: Incompatible row length.
ERROR: Incompatible column space.
10 20 30
40 50 60
python matrix logic digits
I am confused as to why my digits are not printing correctly from my Matrix column.
It is missing the trailing digits after the first digit. However, it performs the correct operations. I am not sure as to why the output is like this and have looked over my code a few times. I may not be catching something.
I would like your help in finding my logic error.
class Matrix:
def __init__(self, Rowsp=[]):
"""Matrix Constructor"""
self.Rowsp = Rowsp
self.Colsp = [[self.Rowsp[j][i] for j in range(len(self.Rowsp))] for i in range(len(self.Rowsp[0]))]
def copy(self):
"""
Returns a copy of the matrix for calculations
"""
copy =
rows = len(self.Rowsp)
cols = len(self.Rowsp[0])
for i in range(rows):
copy.append(list())
for j in range(cols):
copy[i].append(self.Rowsp[i][j])
return Matrix(copy)
Setters
def setCol(self, j, u):
"""
changes the j-th column to be the list u.
If u is not the same length as the existing columns,
then the constructor should raise a ValueError
with the message Incompatible column length.
"""
rows = len(self.Rowsp)
if (len(u) != rows):
#raise ValueError - SWAP OUT FOR VAL ERROR
print("ERROR: Incompatible column length")
else:
j -= 1
for i in range(rows):
self.Rowsp[i][j] = u[i]
def setRow(self, i, v):
"""
changes the i-th row to be the list v.
If v is not the same length as the existing rows,
then the constructor should raise a ValueError
with the message Incompatible row length.
"""
if len(self.Rowsp[0]) != len(v):
#raise ValueError - SWAP OUT FOR VAL ERROR
print("ERROR: Incompatible row length")
else:
self.Rowsp[i - 1] = v
def setEntry(self, i, j, a):
"""
changes the existing a_ij entry in the matrix to a
"""
self.Rowsp[i - 1][j - 1] = a
Getters
def getCol(self, j):
"""
returns the j-th column as a list.
"""
return self.Colsp[j - 1]
def getRow(self, i):
"""
returns the i-th row as a list v.
"""
return self.Rowsp[i - 1]
def getEntry(self, i, j):
"""
returns the existing a_ij entry in the matrix.
"""
return self.Rowsp[i - 1][j - 1]
def getColSpace(self):
"""
returns the list of vectors that make up the column space of the matrix object
"""
c = self.Colsp.copy()
return c
def getRowSpace(self):
"""
returns the list of vectors that make up the row space of the matrix object
"""
r = self.Rowsp.copy()
return r
Test Case
print("The 2nd row is:", A.getRow(2))
print("The 3rd column is:", A.getCol(3))
print()
A.setRow(2, [40, 50])
A.setCol(2, [30, 4, 1])
print(A)
My result:
The 2nd row is: [40, 50, 60]
The 3rd column is: [3, 6]
ERROR: Incompatible row length
ERROR: Incompatible column length
10 20 30
40 50 60
What I should get:
The 2nd row is: [40, 50, 60]
The 3rd column is: [30, 60]
ERROR: Incompatible row length.
ERROR: Incompatible column space.
10 20 30
40 50 60
python matrix logic digits
python matrix logic digits
edited Nov 11 at 23:50
asked Nov 11 at 23:09
Mark
85
85
add a comment |
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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',
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
,
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%2fstackoverflow.com%2fquestions%2f53254165%2fpython-missing-digit-when-printing-out-column%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- 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.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
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%2fstackoverflow.com%2fquestions%2f53254165%2fpython-missing-digit-when-printing-out-column%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