ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 07 Dec 2017 20:31:54 +0100How to create a matrix in sage that expands with a variable number of rows and columns and in each row the values are squared?https://ask.sagemath.org/question/39986/how-to-create-a-matrix-in-sage-that-expands-with-a-variable-number-of-rows-and-columns-and-in-each-row-the-values-are-squared/I need to make a code that takes takes two inputs: n and d and creates a matrix of the values of a list P.
'n' will be the number of columns and 'd' the number of rows.
In each row, I need the values of the following row to be raised to an incrementally rising power.
This is what I have so far:
# inputs
n = 5
d = 5
f = x^2
# list of size n
P = [(i, f(i)) for i in range(n)]
# matrix
C = matrix([(P[i][0]) for i in range(n)])
show(C)Thu, 07 Dec 2017 03:28:33 +0100https://ask.sagemath.org/question/39986/how-to-create-a-matrix-in-sage-that-expands-with-a-variable-number-of-rows-and-columns-and-in-each-row-the-values-are-squared/Comment by dan_fulea for <p>I need to make a code that takes takes two inputs: n and d and creates a matrix of the values of a list P.</p>
<p>'n' will be the number of columns and 'd' the number of rows.</p>
<p>In each row, I need the values of the following row to be raised to an incrementally rising power.</p>
<p>This is what I have so far:</p>
<pre><code># inputs
n = 5
d = 5
f = x^2
# list of size n
P = [(i, f(i)) for i in range(n)]
# matrix
C = matrix([(P[i][0]) for i in range(n)])
show(C)
</code></pre>
https://ask.sagemath.org/question/39986/how-to-create-a-matrix-in-sage-that-expands-with-a-variable-number-of-rows-and-columns-and-in-each-row-the-values-are-squared/?comment=40002#post-id-40002Please mark the `python` and `sage` code as such, just mark it and pres `Control+K`. (Or hit that button with the two rows `101` and `010` in the header of the question area.
The above looks (slightly changed) like:
# define inputs
n, d, f = 5, 5, x^2
# define a list of size n
P = [ (j, f(j)) for j in range(n) ]
# define a matrix
C = ???
Please describe mathematically which is the matrix that should be defined.
Try ?matrix in the sage interpreter to see more examples.Thu, 07 Dec 2017 20:31:54 +0100https://ask.sagemath.org/question/39986/how-to-create-a-matrix-in-sage-that-expands-with-a-variable-number-of-rows-and-columns-and-in-each-row-the-values-are-squared/?comment=40002#post-id-40002Answer by tmonteil for <p>I need to make a code that takes takes two inputs: n and d and creates a matrix of the values of a list P.</p>
<p>'n' will be the number of columns and 'd' the number of rows.</p>
<p>In each row, I need the values of the following row to be raised to an incrementally rising power.</p>
<p>This is what I have so far:</p>
<pre><code># inputs
n = 5
d = 5
f = x^2
# list of size n
P = [(i, f(i)) for i in range(n)]
# matrix
C = matrix([(P[i][0]) for i in range(n)])
show(C)
</code></pre>
https://ask.sagemath.org/question/39986/how-to-create-a-matrix-in-sage-that-expands-with-a-variable-number-of-rows-and-columns-and-in-each-row-the-values-are-squared/?answer=39992#post-id-39992To get some documentation on how to create custom matrices, you can do:
sage: matrix?
You will see some examples using a function taking a pair `(i,j)` and returning some `f(i,j)` to be placed in the entry `[i,j]` of the constructed matrix.
For example, if you want that the entry `[i,j]` has value `i*j^2` (say), you can do:
sage: m = 4 ; n = 5
sage: matrix(QQ, m, n, lambda i, j: i*j^2)
[ 0 0 0 0 0]
[ 0 1 4 9 16]
[ 0 2 8 18 32]
[ 0 3 12 27 48]Thu, 07 Dec 2017 11:16:32 +0100https://ask.sagemath.org/question/39986/how-to-create-a-matrix-in-sage-that-expands-with-a-variable-number-of-rows-and-columns-and-in-each-row-the-values-are-squared/?answer=39992#post-id-39992