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.Wed, 28 Mar 2018 03:58:43 +0200Finding the matrix of a linear affine transformation and its inversehttps://ask.sagemath.org/question/41629/finding-the-matrix-of-a-linear-affine-transformation-and-its-inverse/How do i get the matrix representation of an affine transformation and it's inverse in sage?
I am more so interested in doing this for random affine transformations as i am using them in a multivariate cryptography scheme but for example the following affine transformation over GF(3)
[1 0 0 2 0 2] [1]
[2 2 1 1 2 0] [1]
[2 0 0 2 2 2] [0]
x |-> [2 2 0 1 1 1] x + [1]
[1 0 2 0 2 0] [0]
[0 0 0 2 0 2] [2]Mon, 19 Mar 2018 17:03:59 +0100https://ask.sagemath.org/question/41629/finding-the-matrix-of-a-linear-affine-transformation-and-its-inverse/Comment by slelievre for <p>How do i get the matrix representation of an affine transformation and it's inverse in sage?</p>
<p>I am more so interested in doing this for random affine transformations as i am using them in a multivariate cryptography scheme but for example the following affine transformation over GF(3)</p>
<pre><code> [1 0 0 2 0 2] [1]
[2 2 1 1 2 0] [1]
[2 0 0 2 2 2] [0]
x |-> [2 2 0 1 1 1] x + [1]
[1 0 2 0 2 0] [0]
[0 0 0 2 0 2] [2]
</code></pre>
https://ask.sagemath.org/question/41629/finding-the-matrix-of-a-linear-affine-transformation-and-its-inverse/?comment=41631#post-id-41631Can you provide an example of an affine transformation you are interested in?Mon, 19 Mar 2018 17:07:53 +0100https://ask.sagemath.org/question/41629/finding-the-matrix-of-a-linear-affine-transformation-and-its-inverse/?comment=41631#post-id-41631Comment by Dalvir for <p>How do i get the matrix representation of an affine transformation and it's inverse in sage?</p>
<p>I am more so interested in doing this for random affine transformations as i am using them in a multivariate cryptography scheme but for example the following affine transformation over GF(3)</p>
<pre><code> [1 0 0 2 0 2] [1]
[2 2 1 1 2 0] [1]
[2 0 0 2 2 2] [0]
x |-> [2 2 0 1 1 1] x + [1]
[1 0 2 0 2 0] [0]
[0 0 0 2 0 2] [2]
</code></pre>
https://ask.sagemath.org/question/41629/finding-the-matrix-of-a-linear-affine-transformation-and-its-inverse/?comment=41633#post-id-41633I have edited my question with an example, any help would be appreciated.Mon, 19 Mar 2018 17:43:47 +0100https://ask.sagemath.org/question/41629/finding-the-matrix-of-a-linear-affine-transformation-and-its-inverse/?comment=41633#post-id-41633Comment by kcrisman for <p>How do i get the matrix representation of an affine transformation and it's inverse in sage?</p>
<p>I am more so interested in doing this for random affine transformations as i am using them in a multivariate cryptography scheme but for example the following affine transformation over GF(3)</p>
<pre><code> [1 0 0 2 0 2] [1]
[2 2 1 1 2 0] [1]
[2 0 0 2 2 2] [0]
x |-> [2 2 0 1 1 1] x + [1]
[1 0 2 0 2 0] [0]
[0 0 0 2 0 2] [2]
</code></pre>
https://ask.sagemath.org/question/41629/finding-the-matrix-of-a-linear-affine-transformation-and-its-inverse/?comment=41636#post-id-41636Do you mean like a projective coordinate representation? In that case if you have `Mx+b` you could make a block matrix out of `M` and `b` like `block_matrix([[M,b],[0,1]])` or something, and then get that inverse. But I don't know if this is what you mean by the matrix representation of an affine transformation.Mon, 19 Mar 2018 18:22:25 +0100https://ask.sagemath.org/question/41629/finding-the-matrix-of-a-linear-affine-transformation-and-its-inverse/?comment=41636#post-id-41636Answer by slelievre for <p>How do i get the matrix representation of an affine transformation and it's inverse in sage?</p>
<p>I am more so interested in doing this for random affine transformations as i am using them in a multivariate cryptography scheme but for example the following affine transformation over GF(3)</p>
<pre><code> [1 0 0 2 0 2] [1]
[2 2 1 1 2 0] [1]
[2 0 0 2 2 2] [0]
x |-> [2 2 0 1 1 1] x + [1]
[1 0 2 0 2 0] [0]
[0 0 0 2 0 2] [2]
</code></pre>
https://ask.sagemath.org/question/41629/finding-the-matrix-of-a-linear-affine-transformation-and-its-inverse/?answer=41726#post-id-41726An affine transformation `t` is given by some square matrix `a`
and some vector `b`, and maps `x` to `a * x + b`.
One can represent such a transformation `t` by an augmented matrix,
whose first `n` columns are those of `a` and whose last column has
the entries of `b`. We also denote this matrix by `t`.
Then the `n` first columns represent the linear part ``a`` of the
transformation `t`, and its last column represents the translation
part, the vector `b`.
Since the transformation `t` maps a vector `x` to `a * x + b`,
provided `a` is invertible, with inverse `A`, then so is `t`,
and its inverse `T` maps `x` to `A * x - A * b`.
Explanation: if `y = t(x)`, then `y = a * x + b`, so
`y - b = a * x`, and if `a` is invertible with inverse `A`,
then `A * y + A * (-b) = A * a * x`, which means
`x = `A * y + A * (-b)`, so the linear part is `A`
and the translation part is `A * (-b)`.
Therefore the matrix for `t_inv`, also denoted as `T`, has
`A` as its first `n` columns and `A * (-b)` as its last column.
Here is an implementation of a function that returns the inverse of an
affine transformation. The function includes a documentation string which
summarizes the discussion above, and an example based on the one in
your question.
def affine_inverse(t, as_block_matrix=False):
"""
Return the inverse of this affine transformation
The affine transformation is given by some `n` by `(n + 1)`
matrix `t` whose `n` first columns represent the linear
part `a` of the transformation, and whose last column represents
the vector `b`, so that the transformation maps a vector `x`
to `a * x + b`. Provided `a` is invertible, with inverse `A`,
then so is `t`, and its inverse `T` maps `x` to `A * x - A * b`.
Optionally, `T` is returned as a block matrix.
EXAMPLE::
sage: k = GF(3)
sage: alist = [
....: [1, 0, 0, 2, 0, 2],
....: [2, 2, 1, 1, 2, 0],
....: [2, 0, 0, 2, 2, 2],
....: [2, 2, 0, 1, 1, 1],
....: [1, 0, 2, 0, 2, 0],
....: [0, 0, 0, 2, 0, 2],
....: ]
sage: blist = (1, 1, 0, 1, 0, 2)
sage: a = matrix(k, alist)
sage: n = a.nrows()
sage: b = matrix(k, n, 1, blist)
sage: t = a.augment(b)
sage: affine_inverse(t)
[1 0 0 0 0 2 1]
[1 0 2 2 0 2 2]
[2 0 1 0 2 0 1]
[2 1 0 2 1 0 1]
[2 0 2 0 0 2 0]
[1 2 0 1 2 2 1]
sage: affine_inverse(t, as_block_matrix=True)
[1 0 0 0 0 2|1]
[1 0 2 2 0 2|2]
[2 0 1 0 2 0|1]
[2 1 0 2 1 0|1]
[2 0 2 0 0 2|0]
[1 2 0 1 2 2|1]
"""
k = t.base_ring()
n = t.nrows()
a = t.delete_columns([n])
b = t.delete_columns(range(n))
A = ~a
if as_block_matrix:
return block_matrix(k, 1, 2, [A, A * -b])
return A.augment(A * -b)
Fri, 23 Mar 2018 01:09:29 +0100https://ask.sagemath.org/question/41629/finding-the-matrix-of-a-linear-affine-transformation-and-its-inverse/?answer=41726#post-id-41726Comment by kcrisman for <p>An affine transformation <code>t</code> is given by some square matrix <code>a</code>
and some vector <code>b</code>, and maps <code>x</code> to <code>a * x + b</code>.</p>
<p>One can represent such a transformation <code>t</code> by an augmented matrix,
whose first <code>n</code> columns are those of <code>a</code> and whose last column has
the entries of <code>b</code>. We also denote this matrix by <code>t</code>.</p>
<p>Then the <code>n</code> first columns represent the linear part <code>a</code> of the
transformation <code>t</code>, and its last column represents the translation
part, the vector <code>b</code>.</p>
<p>Since the transformation <code>t</code> maps a vector <code>x</code> to <code>a * x + b</code>,
provided <code>a</code> is invertible, with inverse <code>A</code>, then so is <code>t</code>,
and its inverse <code>T</code> maps <code>x</code> to <code>A * x - A * b</code>.</p>
<p>Explanation: if <code>y = t(x)</code>, then <code>y = a * x + b</code>, so
<code>y - b = a * x</code>, and if <code>a</code> is invertible with inverse <code>A</code>,
then <code>A * y + A * (-b) = A * a * x</code>, which means
<code>x =</code>A * y + A * (-b)<code>, so the linear part is</code>A<code>
and the translation part is</code>A * (-b)`.</p>
<p>Therefore the matrix for <code>t_inv</code>, also denoted as <code>T</code>, has
<code>A</code> as its first <code>n</code> columns and <code>A * (-b)</code> as its last column.</p>
<p>Here is an implementation of a function that returns the inverse of an
affine transformation. The function includes a documentation string which
summarizes the discussion above, and an example based on the one in
your question.</p>
<pre><code>def affine_inverse(t, as_block_matrix=False):
"""
Return the inverse of this affine transformation
The affine transformation is given by some `n` by `(n + 1)`
matrix `t` whose `n` first columns represent the linear
part `a` of the transformation, and whose last column represents
the vector `b`, so that the transformation maps a vector `x`
to `a * x + b`. Provided `a` is invertible, with inverse `A`,
then so is `t`, and its inverse `T` maps `x` to `A * x - A * b`.
Optionally, `T` is returned as a block matrix.
EXAMPLE::
sage: k = GF(3)
sage: alist = [
....: [1, 0, 0, 2, 0, 2],
....: [2, 2, 1, 1, 2, 0],
....: [2, 0, 0, 2, 2, 2],
....: [2, 2, 0, 1, 1, 1],
....: [1, 0, 2, 0, 2, 0],
....: [0, 0, 0, 2, 0, 2],
....: ]
sage: blist = (1, 1, 0, 1, 0, 2)
sage: a = matrix(k, alist)
sage: n = a.nrows()
sage: b = matrix(k, n, 1, blist)
sage: t = a.augment(b)
sage: affine_inverse(t)
[1 0 0 0 0 2 1]
[1 0 2 2 0 2 2]
[2 0 1 0 2 0 1]
[2 1 0 2 1 0 1]
[2 0 2 0 0 2 0]
[1 2 0 1 2 2 1]
sage: affine_inverse(t, as_block_matrix=True)
[1 0 0 0 0 2|1]
[1 0 2 2 0 2|2]
[2 0 1 0 2 0|1]
[2 1 0 2 1 0|1]
[2 0 2 0 0 2|0]
[1 2 0 1 2 2|1]
"""
k = t.base_ring()
n = t.nrows()
a = t.delete_columns([n])
b = t.delete_columns(range(n))
A = ~a
if as_block_matrix:
return block_matrix(k, 1, 2, [A, A * -b])
return A.augment(A * -b)
</code></pre>
https://ask.sagemath.org/question/41629/finding-the-matrix-of-a-linear-affine-transformation-and-its-inverse/?comment=41800#post-id-41800Okay, so it is what I thought. Nice example.Wed, 28 Mar 2018 03:58:43 +0200https://ask.sagemath.org/question/41629/finding-the-matrix-of-a-linear-affine-transformation-and-its-inverse/?comment=41800#post-id-41800