How do you leave the sqrt(2) in a matrix for the text part of a question:
$U = Matrix([[1/sqrt(2),-1/sqrt(2)],
[1/sqrt(2),1/sqrt(2)]]);
I want the students to see \begin{bmatrix} \frac{1}{\sqrt{2} ... \end{bmatrix}
when I ask the question. I am not sure how to leave these squareroots as displayed.
I know this must be simple but I can't seem to find the answer anywhere.
In reply to Jack Dockery
Re: How to in Matrix context leave un evaluated expressions
by Danny Glin -
Have you tried
$U = Matrix([["1/sqrt(2)","-1/sqrt(2)"],
["1/sqrt(2)","1/sqrt(2)"]]);
Without the quotes, you are building a Matrix object based on perl reals. With the quotes, MathObjects will parse the strings, and hopefully maintain the structure.
In reply to Danny Glin
Re: How to in Matrix context leave un evaluated expressions
by Jack Dockery -
I tied it but no luck ....
To be clear, I would like to have a symbolic matrix that matches the matrix I am computing with so as to prevent asking the students a different question in text
then the one I am asking in the answers. Below is an example and if I change the
matrix in the matrix context, $U and don't or forget to change the latex displayed matrix in the \bmatrix bit, the students will be doing what I ask in the displayed problem but will not get the correct answers if $U doesn't match.
If the two versions are side by side in the preamble that would be ok also....
DOCUMENT();
loadMacros("PG.pl",
"PGbasicmacros.pl",
"PGstandard.pl",
"PGML.pl",
"MathObjects.pl",
"PGcourse.pl",
#"PGmatrixmacros.pl",
# "parserPopUp.pl",
#"parserVectorUtils.pl",
);
TEXT(beginproblem());
$showPartialCorrectAnswers = 1;
######################################################################
#SRAND($psvn);
$seed = random(1,100);
SRAND($seed);
##################################
#
# Setup
#
#
Context("Matrix");
#Set up the SVD decomposition
$U = Matrix([[1/sqrt(2),-1/sqrt(2)],
[1/sqrt(2),1/sqrt(2)]]);
$V = Matrix([[0,1],
[1,0]]);
$S = Matrix([[Compute(random(4,6)),0],
[0,Compute(non_zero_random(1,3))]]);
$VT = $V->transpose;
$UT=$U->transpose;
$SI=$S->inverse;
$A = $U*$S*$VT;
#Randomly generate x
$x = sqrt(2)*Matrix([[Compute(non_zero_random(-2,2))],[Compute(non_zero_random(1,3))]]);
$b = $A*$x;
$z=$UT*$b;
$y=$SI*$z;
BEGIN_PGML
Use the given SVD factorization of [` A = U S V^{T} `]
[``A = \begin{bmatrix} \frac{1}{\sqrt{2}} & \frac{-1}{\sqrt{2}} \\
\frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \end{bmatrix} [$S] [$V]^{T}``]
Solve [` A x = [$b] `] by completing the three steps outlined in the text:
Step 1: Letting [` z = S V^{T} x`], [` A x = b `] is the same as
[` U z = [$b]`]. Since [` U `] is orthogonal, it follows that
[`` z = U^{T} b ``] = [___]*{$z}
Step 2: We now have [` S V^{T} x = U^{T} b = z`] so letting
[` y = V^{T} x `], we have [` S y = U^{T}b = z `], it follows that
[`` y = S^{-1} z ``] = [___]*{$y}
Step 3: And finally since [` V^{T} x = y `], we see that
[``x = V y ``] = [___]*{$x}.
END_PGML
ENDDOCUMENT();
loadMacros("PG.pl",
"PGbasicmacros.pl",
"PGstandard.pl",
"PGML.pl",
"MathObjects.pl",
"PGcourse.pl",
#"PGmatrixmacros.pl",
# "parserPopUp.pl",
#"parserVectorUtils.pl",
);
TEXT(beginproblem());
$showPartialCorrectAnswers = 1;
######################################################################
#SRAND($psvn);
$seed = random(1,100);
SRAND($seed);
##################################
#
# Setup
#
#
Context("Matrix");
#Set up the SVD decomposition
$U = Matrix([[1/sqrt(2),-1/sqrt(2)],
[1/sqrt(2),1/sqrt(2)]]);
$V = Matrix([[0,1],
[1,0]]);
$S = Matrix([[Compute(random(4,6)),0],
[0,Compute(non_zero_random(1,3))]]);
$VT = $V->transpose;
$UT=$U->transpose;
$SI=$S->inverse;
$A = $U*$S*$VT;
#Randomly generate x
$x = sqrt(2)*Matrix([[Compute(non_zero_random(-2,2))],[Compute(non_zero_random(1,3))]]);
$b = $A*$x;
$z=$UT*$b;
$y=$SI*$z;
BEGIN_PGML
Use the given SVD factorization of [` A = U S V^{T} `]
[``A = \begin{bmatrix} \frac{1}{\sqrt{2}} & \frac{-1}{\sqrt{2}} \\
\frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \end{bmatrix} [$S] [$V]^{T}``]
Solve [` A x = [$b] `] by completing the three steps outlined in the text:
Step 1: Letting [` z = S V^{T} x`], [` A x = b `] is the same as
[` U z = [$b]`]. Since [` U `] is orthogonal, it follows that
[`` z = U^{T} b ``] = [___]*{$z}
Step 2: We now have [` S V^{T} x = U^{T} b = z`] so letting
[` y = V^{T} x `], we have [` S y = U^{T}b = z `], it follows that
[`` y = S^{-1} z ``] = [___]*{$y}
Step 3: And finally since [` V^{T} x = y `], we see that
[``x = V y ``] = [___]*{$x}.
END_PGML
ENDDOCUMENT();
In reply to Jack Dockery
Re: How to in Matrix context leave un evaluated expressions
by Alex Jordan -This works for me in WW 2.14. But I had to use a Formula MathObject, not a Matrix. Depending on what else you are doing with $U, that may not be good.
EDIT: Now I see your more recent post. You are using things like the transpose method, which I will assume do not work on a Formula. But you could get around this by doing like:
$UforDisplay = Formula(...);
$U = Matrix("$UforDisplay");
and use them separately.
DOCUMENT();
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
);
Context("Matrix")->flags->set(reduceConstants=>0,reduceConstantFunctions=>0);
$U = Formula("[
[sqrt(2),sqrt(2)],
[-sqrt(2),sqrt(2)]
]");
BEGIN_PGML
[`[$U]`]
END_PGML
ENDDOCUMENT();
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
);
Context("Matrix")->flags->set(reduceConstants=>0,reduceConstantFunctions=>0);
$U = Formula("[
[sqrt(2),sqrt(2)],
[-sqrt(2),sqrt(2)]
]");
BEGIN_PGML
[`[$U]`]
END_PGML
ENDDOCUMENT();