#๐ Putting Matrices all on One Line
149 messages ยท Page 1 of 1 (latest)
@winged prairie
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
is that all your code?
i've never worked with np, but if it is to do with python and not np functions im happy to help
What?
just taggin random ppl ๐ญ
I need some help with putting all the matrices one line as show in my screenshot
@deep belfry he has helped me out in the past
row_echelon_M
@winged prairie no one is on-call to help you at any time, even if they've helped you in the past. so don't ping people to summon them to your question
Do get it
def gaussian_elimination(A, B):
"""
Solve a linear system represented by an augmented matrix using the Gaussian elimination method.
Parameters:
- A (numpy.array): Square matrix of size n x n representing the coefficients of the linear system
- B (numpy.array): Column matrix of size 1 x n representing the constant terms.
Returns:
numpy.array or str: The solution vector if a unique solution exists, or a string indicating the type of solution.
"""
### START CODE HERE ###
# Get the matrix in row echelon form
row_echelon_M = np.linalg.solve(A,B)
# If the system is non-singular, then perform back substitution to get the result.
# Since the function row_echelon_form returns a string if there is no solution, let's check for that.
# The function isinstance checks if the first argument has the type as the second argument, returning True if it does and False otherwise.
if not isinstance(row_echelon_M, str):
return row_echelon_M
### END SOLUTION HERE ###
return solution
just convert it to a string?
If you can show me with the code as justr do the str() then
i've never used numpy, i dont know where the list is
no
Okay
The arrays aren't correct.
Then how should they be then
It should be 1d not 2d.
A = np.array([[1,2,3], [3,4,5], [4,5,6]])
B = np.array([[1], [5], [7]])
print(augmented_matrix(A,B))
So I am wondering how I can do that
@split pilot
I'm confused. Why is it augmented_matrix now instead of gaussian_elimination?
It is a different function call as it does the same idea
def augmented_matrix(A, B):
"""
Create an augmented matrix by horizontally stacking two matrices A and B.
Parameters:
- A (numpy.array): First matrix.
- B (numpy.array): Second matrix.
Returns:
- numpy.array: Augmented matrix obtained by horizontally stacking A and B.
"""
augmented_M = np.hstack((A,B))
return augmented_M
There the code for it but am trying to focus on the gaussian_elimination
Yes, but augmented_matrix returns an actual 2d array here: 3x4
On the other hand gaussian_elimination returns a 5x1 array when it's supposed to return a vector of length 5.
You could .ravel() the matrix before returning it.
A = np.array([[1,2,3], [3,4,5], [4,5,6]])
B = np.array([[1], [5], [7]]).ravel()
Like that
I think return row_echelon_m.ravel()
That worked
This took 3 weeks to solve it
@split pilot thank you so much and would it be okay I can ask you future help
I think it makes no sense to ask specific people for help. Better just put your question in a help channel.
Okay as thank you
here was an error grading your submission. Details:
operands could not be broadcast together with shapes (8,) (4,)
w2_unittest.test_gaussian_elimination(gaussian_elimination)
I am getting this erro from this funciton
What does that mean?
In this question on stackoverflow the OP tried to add a 1d array and a 2d array. Of course that's not possible.
Do you try to add arrays in gaussian_elimination?
No not at all
def gaussian_elimination(A, B):
"""
Solve a linear system represented by an augmented matrix using the Gaussian elimination method.
Parameters:
- A (numpy.array): Square matrix of size n x n representing the coefficients of the linear system
- B (numpy.array): Column matrix of size 1 x n representing the constant terms.
Returns:
numpy.array or str: The solution vector if a unique solution exists, or a string indicating the type of solution.
"""
### START CODE HERE ###
# Get the matrix in row echelon form
row_echelon_M = np.linalg.solve(A, B)
# If the system is non-singular, then perform back substitution to get the result.
# Since the function row_echelon_form returns a string if there is no solution, let's check for that.
# The function isinstance checks if the first argument has the type as the second argument, returning True if it does and False otherwise.
if not isinstance(row_echelon_M, str):
return row_echelon_M.ravel()
### END SOLUTION HERE ###
return solution
equations = [
[3, 6, 6, 8, 1],
[5, 3, 6, -10],
[4,- 5,8,8],
[4, 0,0, 0, 8,9]]
variables, A, B = string_to_augmented_matrix(equations)
sols = gaussian_elimination(A, B).split(',')
if not isinstance(sols, str):
for variable, solution in zip(variables,sols):
print(f"{variable} = {solution:.4f}")
else:
print(sols)
Sorry as lets solve one at a time
numpy arrays don't have a split method.
Interesting. But equations isn't a string.
This coming rom a coursera course as no idea why for this error
I think your equations already are an augmented matrix. Could that be?
Probably
Probably you did something wrong then. Maybe you should find out what this function does.
I will
equations = [
[3, 6, 6, 8, 1],
[5, 3, 6, -10],
[4,- 5,8,8],
[4, 0,0, 0, 8,9]]
variables, A, B = string_to_augmented_matrix(equations)
sols = gaussian_elimination(A, B)
if not isinstance(sols, str):
for variable, solution in zip(variables,sols):
print(f"{variable} = {solution:.4f}")
else:
print(sols)
from utils import string_to_augmented_matrix
The code below will allow you to write any equation in the format it is given below (any unknown lower case variables are accepted, in any order) and transform it in its respective augmented matrix so you can solve it using the functions you just wrote in this assignment!
You just need to change the equations variable, always keeping * to indicate product between unknowns and variables and one equation in each line!
What format is given below?
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-40-a6ca3511c2c5> in <module>
5 [4, 0,0, 0, 8,9]]
6
----> 7 variables, A, B = string_to_augmented_matrix(equations)
8
9 sols = gaussian_elimination(A, B)
~/work/utils.py in string_to_augmented_matrix(equations)
4 def string_to_augmented_matrix(equations):
5 # Split the input string into individual equations
----> 6 equation_list = equations.split('\n')
7 equation_list = [x for x in equation_list if x != '']
8 # Create a list to store the coefficients and constants
AttributeError: 'list' object has no attribute 'split'
There is no function defintion
variables, A, B = string_to_augmented_matrix(equations)
Yes, lists don't have a split method.
Do know as I think it just the grading system itself is the main issue
Can you help me with back subsittuin then
!pastebin
If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.
There is the equation itself
Expected:
[-0.0841373 0.12363912 0.88135387 -2.83469018 -0.27829698 -3.87730702],
but got:
[-0.30941613 1.30319901 4.69817804 0.13843141 0.74359276 -3.87730702].
The numbers are wrong.
Yes and so how can I do the right caluclations then for the equation
A = np.array([[1,2,3],[0,1,0], [0,0,5]])
B = np.array([[1], [2], [4]])
row_echelon_form(A,B)
w2_unittest.test_row_echelon_form(row_echelon_form)
As I said earlier, pinging people to summon them to your help is not allowed
I think there is a error in the formula.
Well, how does back substitution work?
No it does not as got the wrong numbers with the formula as no idea what the main issue could be
That is because it does the wrong calculations.
What is this line of code good for? row_to_reduce = row_to_reduce - value * substitution_row
Trying to do
A = np.array([[1,2,3],[0,0,0], [0,0,5]])
B = np.array([[1], [2], [4]])
reduced_row_echelon_form(A,B)
array([[1., 2., 3., 1.],
[0., 0., 0., 1.],
[0., 0., 1., 0.]])
M[j] = M[j] - value_below_pivot * M[row]
Hm. But why would you multiply the M[row] with the value? I don't see how that makes sense.
What should I use then
What do your debug prints say?
array([[1. , 2. , 3. , 1. ],
[0. , 1. , 0. , 2. ],
[0. , 0. , 1. , 0.8]])
All tests passed
I did try put it shows the wrong numbrs
array([[1., 2., 3., 1.],
[0., 0., 0., 1.],
[0., 0., 1., 0.]])
It should be show thing
I mean you have this debug print print(f"DEBUG: end result = \n{M}.")
It should show the final matrix after back substitution. And this matrix should consist mainly of zeroes.
[ 0. 0. 1. 1.]].
DEBUG: viewing row 1 ([ 0. 1. 0. -2.]) index 1 (1.0).
DEBUG: reducing row 0 ([1. 8. 0. 3.]) index 0 (1.0).
DEBUG: reducing using value = 8.0.
DEBUG: end result =
[[ 1. 0. 0. 19.]
[ 0. 1. 0. -2.]
[ 0. 0. 1. 1.]].
DEBUG: viewing row 0 ([ 1. 0. 0. 19.]) index 0 (1.0).
DEBUG: end result =
[[ 1. 0. 0. 19.]
[ 0. 1. 0. -2.]
[ 0. 0. 1. 1.]].
So the bug must be elsewhere.
Yes
Or maybe the function works for some test input, but not all test inputs. As the function makes some assumptions.
The function assumes that the first non-zero value of each row is a 1.0.
If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.
The suggestion is to satisfy that assumption.
I acually got 100% on that as it now works as back_subtitution is the main issue now
But it returns a Nx1 matrix instead of a vector. Not sure if that's correct.
Okay then how should I fix it then
Ah, I'm sorry. I read that wrong. It actually returns a vector.
So everything works. Great.
Yes
Thank you again for the help and why spend this long with me
.close
!close
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.