Making an interactive visualization of the eigenvectors of two-dimensional matrices









up vote
9
down vote

favorite
5












I've recently stumbled upon this very nice interactive visualization of eigenvectors of two-dimensional matrices, and how powers $A^k$ act on various vectors.



How can this sort of visualization be realized with Mathematica, leveraging its dynamical capabilities?










share|improve this question























  • Maybe not what you want, but there are interactive examples at demonstrations.wolfram.com, such as this and this
    – Daniel Lichtblau
    Nov 10 at 15:55














up vote
9
down vote

favorite
5












I've recently stumbled upon this very nice interactive visualization of eigenvectors of two-dimensional matrices, and how powers $A^k$ act on various vectors.



How can this sort of visualization be realized with Mathematica, leveraging its dynamical capabilities?










share|improve this question























  • Maybe not what you want, but there are interactive examples at demonstrations.wolfram.com, such as this and this
    – Daniel Lichtblau
    Nov 10 at 15:55












up vote
9
down vote

favorite
5









up vote
9
down vote

favorite
5






5





I've recently stumbled upon this very nice interactive visualization of eigenvectors of two-dimensional matrices, and how powers $A^k$ act on various vectors.



How can this sort of visualization be realized with Mathematica, leveraging its dynamical capabilities?










share|improve this question















I've recently stumbled upon this very nice interactive visualization of eigenvectors of two-dimensional matrices, and how powers $A^k$ act on various vectors.



How can this sort of visualization be realized with Mathematica, leveraging its dynamical capabilities?







graphics dynamic visualization eigenvalues eventhandler






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 1:08

























asked Nov 9 at 0:44









glS

4,91911142




4,91911142











  • Maybe not what you want, but there are interactive examples at demonstrations.wolfram.com, such as this and this
    – Daniel Lichtblau
    Nov 10 at 15:55
















  • Maybe not what you want, but there are interactive examples at demonstrations.wolfram.com, such as this and this
    – Daniel Lichtblau
    Nov 10 at 15:55















Maybe not what you want, but there are interactive examples at demonstrations.wolfram.com, such as this and this
– Daniel Lichtblau
Nov 10 at 15:55




Maybe not what you want, but there are interactive examples at demonstrations.wolfram.com, such as this and this
– Daniel Lichtblau
Nov 10 at 15:55










1 Answer
1






active

oldest

votes

















up vote
17
down vote













The following is an attempt to recreate a similar sort of interactive visualization, showing the eigenvectors (when real), and how the various points of the unit circle are transformed by the matrix.



The matrix can be chosen by moving its two column vectors using the mouse. I used EventHandler for this, instead of Locators, for greater customizability and a more natural look.



To ease code readability and modularity, the components of the graphical object are defined separately in a private context, and injected into the final DynamicModule object.



Here is the full code:



BeginPackage["eigenvectorRepresentation`"];
dynamicalEigenvectorsRepresentation;
Begin["`Private`"];

Attributes[hold] = HoldAllComplete;
ClearAll@injectAndRelease;
Attributes[injectAndRelease] = HoldAllComplete;
injectAndRelease[x_, replacementRules_, hold_: hold] :=
Hold@x /. replacementRules /. hold[s__] :> s // ReleaseHold;

redPoint = hold@
Red, If[TrueQ[movingPointIndex == 1], PointSize@0.04,
PointSize@0.03],
Point@v1, Arrow@0, 0, v1
;

greenPoint = hold@
Green,
If[TrueQ[movingPointIndex == 2], PointSize@0.04, PointSize@0.03],
Point@v2, Arrow@0, 0, v2
;

bluePointAndArrows =
hold@Dynamic@Blue, PointSize@0.03, Point@v3, Arrowheads@0.02,
Arrow /@
Partition[NestList[Dot[matrix, #] &, v3, numOfIterations], 2,
1]
;

showEigenvectors = hold@Dynamic@With[eigs = Eigenvectors@N@matrix,
If[MatchQ[eigs, __Real ..], Purple, Thickness@0.01,
InfiniteLine@-#, # & /@ eigs, ]
];

principalAxes = hold@With[
singularVectors = Transpose@#[[1]], #[[3]] &@
SingularValueDecomposition@matrix,
Map[Thick, Orange, Arrow@0, 0, # &, singularVectors[[1]]],
Map[Thick, Cyan, Arrow@0, 0, # &, singularVectors[[2]]]
];

additionalInfo = hold[
Column@
"PlotRange",
VerticalSlider[Dynamic@frameSize, 1, 10, 0.01,
Appearance -> "Labeled"]
, " ",
Column@
"Number of iterations",
VerticalSlider[Dynamic@numOfIterations, 1, 40, 1,
Appearance -> "Labeled"]

];

eigenvaluesDisplay = hold[
" ",
Dynamic@With[eigvals = Eigenvalues@matrix,
Graphics[Circle, Point@0, 0, Thick,
Arrow@0, 0, ReIm@eigvals[[1]],
Arrow@0, 0, ReIm@eigvals[[2]]
, Axes -> True, PlotRangePadding -> 0.1,
PlotRange -> -1, 1, -1, 1, ImageSize -> 200,
PlotLabel -> "Eigenvalues"]
]
];

arrowRepresentationActionMatrix[matrix_] :=
With[pts = MeshCoordinates@DiscretizeRegion@Region@Circle,
With[finalPts = Dot[matrix, #] & /@ pts,
Graphics[
PointSize@0.012, Point@finalPts,
Arrow /@ Thread@pts, finalPts
]
]];

Options[dynamicalEigenvectorsRepresentation] =
"ShowBluePointAndArrows" -> True,
"ShowEigenvectorsWhenReal" -> True,
"ShowEigenvalues" -> True,
"ShowPrincipalAxes" -> True
;
dynamicalEigenvectorsRepresentation[OptionsPattern] := DynamicModule[
v1 = 0.7, -0.6, v2 = 0.6, 0.6, v3 = 1, 1, movingPointIndex,
matrix, frameSize = 1.5, numOfIterations = 30,
Row[
EventHandler[
Dynamic[
matrix = Transpose@v1, v2;
Show[
arrowRepresentationActionMatrix@matrix,
Graphics[
PointSize@0.02, Circle, Point@0, 0,
"RedPoint", "GreenPoint", "BluePoint",
"ConditionallyShowEigenvectors",
"PrincipalAxes"
],
Frame -> True,
PlotRange -> Dynamic[-#, #, -#, # &@frameSize],
ImageSize -> 500
]
],
"MouseDown" :> With[mp = MousePosition["Graphics"],

movingPointIndex =
Position[v1, v2, v3, First@Nearest[v1, v2, v3, mp]][[1,
1]]
],
"MouseUp" :> (movingPointIndex = 0),
"MouseDragged" :> ReleaseHold[

Hold[Set][Hold[v1, v2, v3][[movingPointIndex]],
MousePosition["Graphics"]]
]
],
"AdditionalInfoSlot",
"EigenvaluesDisplay"
]
]~injectAndRelease~
"RedPoint" -> redPoint, "GreenPoint" -> greenPoint,
"BluePoint" ->
If[OptionValue@"ShowBluePointAndArrows" === True,
bluePointAndArrows, ],
"AdditionalInfoSlot" -> additionalInfo,
"EigenvaluesDisplay" ->
Sequence @@
If[OptionValue@"ShowEigenvalues" ===
True, eigenvaluesDisplay, ],
"ConditionallyShowEigenvectors" ->
If[OptionValue@"ShowEigenvectorsWhenReal" === True,
showEigenvectors, ],
"PrincipalAxes" ->
Sequence @@
If[OptionValue@"ShowPrincipalAxes" === True, principalAxes, ]
;

End;
EndPackage;


Then to create the representation just use



dynamicalEigenvectorsRepresentation[
"ShowEigenvectorsWhenReal" -> True,
"ShowBluePointAndArrows" -> True,
"ShowEigenvalues" -> True,
"ShowPrincipalAxes" -> False
]


and this is the result:



enter image description here






share|improve this answer


















  • 2




    This is really cool
    – user6014
    Nov 9 at 1:03










Your Answer





StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
);
);
, "mathjax-editing");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "387"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f185644%2fmaking-an-interactive-visualization-of-the-eigenvectors-of-two-dimensional-matri%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
17
down vote













The following is an attempt to recreate a similar sort of interactive visualization, showing the eigenvectors (when real), and how the various points of the unit circle are transformed by the matrix.



The matrix can be chosen by moving its two column vectors using the mouse. I used EventHandler for this, instead of Locators, for greater customizability and a more natural look.



To ease code readability and modularity, the components of the graphical object are defined separately in a private context, and injected into the final DynamicModule object.



Here is the full code:



BeginPackage["eigenvectorRepresentation`"];
dynamicalEigenvectorsRepresentation;
Begin["`Private`"];

Attributes[hold] = HoldAllComplete;
ClearAll@injectAndRelease;
Attributes[injectAndRelease] = HoldAllComplete;
injectAndRelease[x_, replacementRules_, hold_: hold] :=
Hold@x /. replacementRules /. hold[s__] :> s // ReleaseHold;

redPoint = hold@
Red, If[TrueQ[movingPointIndex == 1], PointSize@0.04,
PointSize@0.03],
Point@v1, Arrow@0, 0, v1
;

greenPoint = hold@
Green,
If[TrueQ[movingPointIndex == 2], PointSize@0.04, PointSize@0.03],
Point@v2, Arrow@0, 0, v2
;

bluePointAndArrows =
hold@Dynamic@Blue, PointSize@0.03, Point@v3, Arrowheads@0.02,
Arrow /@
Partition[NestList[Dot[matrix, #] &, v3, numOfIterations], 2,
1]
;

showEigenvectors = hold@Dynamic@With[eigs = Eigenvectors@N@matrix,
If[MatchQ[eigs, __Real ..], Purple, Thickness@0.01,
InfiniteLine@-#, # & /@ eigs, ]
];

principalAxes = hold@With[
singularVectors = Transpose@#[[1]], #[[3]] &@
SingularValueDecomposition@matrix,
Map[Thick, Orange, Arrow@0, 0, # &, singularVectors[[1]]],
Map[Thick, Cyan, Arrow@0, 0, # &, singularVectors[[2]]]
];

additionalInfo = hold[
Column@
"PlotRange",
VerticalSlider[Dynamic@frameSize, 1, 10, 0.01,
Appearance -> "Labeled"]
, " ",
Column@
"Number of iterations",
VerticalSlider[Dynamic@numOfIterations, 1, 40, 1,
Appearance -> "Labeled"]

];

eigenvaluesDisplay = hold[
" ",
Dynamic@With[eigvals = Eigenvalues@matrix,
Graphics[Circle, Point@0, 0, Thick,
Arrow@0, 0, ReIm@eigvals[[1]],
Arrow@0, 0, ReIm@eigvals[[2]]
, Axes -> True, PlotRangePadding -> 0.1,
PlotRange -> -1, 1, -1, 1, ImageSize -> 200,
PlotLabel -> "Eigenvalues"]
]
];

arrowRepresentationActionMatrix[matrix_] :=
With[pts = MeshCoordinates@DiscretizeRegion@Region@Circle,
With[finalPts = Dot[matrix, #] & /@ pts,
Graphics[
PointSize@0.012, Point@finalPts,
Arrow /@ Thread@pts, finalPts
]
]];

Options[dynamicalEigenvectorsRepresentation] =
"ShowBluePointAndArrows" -> True,
"ShowEigenvectorsWhenReal" -> True,
"ShowEigenvalues" -> True,
"ShowPrincipalAxes" -> True
;
dynamicalEigenvectorsRepresentation[OptionsPattern] := DynamicModule[
v1 = 0.7, -0.6, v2 = 0.6, 0.6, v3 = 1, 1, movingPointIndex,
matrix, frameSize = 1.5, numOfIterations = 30,
Row[
EventHandler[
Dynamic[
matrix = Transpose@v1, v2;
Show[
arrowRepresentationActionMatrix@matrix,
Graphics[
PointSize@0.02, Circle, Point@0, 0,
"RedPoint", "GreenPoint", "BluePoint",
"ConditionallyShowEigenvectors",
"PrincipalAxes"
],
Frame -> True,
PlotRange -> Dynamic[-#, #, -#, # &@frameSize],
ImageSize -> 500
]
],
"MouseDown" :> With[mp = MousePosition["Graphics"],

movingPointIndex =
Position[v1, v2, v3, First@Nearest[v1, v2, v3, mp]][[1,
1]]
],
"MouseUp" :> (movingPointIndex = 0),
"MouseDragged" :> ReleaseHold[

Hold[Set][Hold[v1, v2, v3][[movingPointIndex]],
MousePosition["Graphics"]]
]
],
"AdditionalInfoSlot",
"EigenvaluesDisplay"
]
]~injectAndRelease~
"RedPoint" -> redPoint, "GreenPoint" -> greenPoint,
"BluePoint" ->
If[OptionValue@"ShowBluePointAndArrows" === True,
bluePointAndArrows, ],
"AdditionalInfoSlot" -> additionalInfo,
"EigenvaluesDisplay" ->
Sequence @@
If[OptionValue@"ShowEigenvalues" ===
True, eigenvaluesDisplay, ],
"ConditionallyShowEigenvectors" ->
If[OptionValue@"ShowEigenvectorsWhenReal" === True,
showEigenvectors, ],
"PrincipalAxes" ->
Sequence @@
If[OptionValue@"ShowPrincipalAxes" === True, principalAxes, ]
;

End;
EndPackage;


Then to create the representation just use



dynamicalEigenvectorsRepresentation[
"ShowEigenvectorsWhenReal" -> True,
"ShowBluePointAndArrows" -> True,
"ShowEigenvalues" -> True,
"ShowPrincipalAxes" -> False
]


and this is the result:



enter image description here






share|improve this answer


















  • 2




    This is really cool
    – user6014
    Nov 9 at 1:03














up vote
17
down vote













The following is an attempt to recreate a similar sort of interactive visualization, showing the eigenvectors (when real), and how the various points of the unit circle are transformed by the matrix.



The matrix can be chosen by moving its two column vectors using the mouse. I used EventHandler for this, instead of Locators, for greater customizability and a more natural look.



To ease code readability and modularity, the components of the graphical object are defined separately in a private context, and injected into the final DynamicModule object.



Here is the full code:



BeginPackage["eigenvectorRepresentation`"];
dynamicalEigenvectorsRepresentation;
Begin["`Private`"];

Attributes[hold] = HoldAllComplete;
ClearAll@injectAndRelease;
Attributes[injectAndRelease] = HoldAllComplete;
injectAndRelease[x_, replacementRules_, hold_: hold] :=
Hold@x /. replacementRules /. hold[s__] :> s // ReleaseHold;

redPoint = hold@
Red, If[TrueQ[movingPointIndex == 1], PointSize@0.04,
PointSize@0.03],
Point@v1, Arrow@0, 0, v1
;

greenPoint = hold@
Green,
If[TrueQ[movingPointIndex == 2], PointSize@0.04, PointSize@0.03],
Point@v2, Arrow@0, 0, v2
;

bluePointAndArrows =
hold@Dynamic@Blue, PointSize@0.03, Point@v3, Arrowheads@0.02,
Arrow /@
Partition[NestList[Dot[matrix, #] &, v3, numOfIterations], 2,
1]
;

showEigenvectors = hold@Dynamic@With[eigs = Eigenvectors@N@matrix,
If[MatchQ[eigs, __Real ..], Purple, Thickness@0.01,
InfiniteLine@-#, # & /@ eigs, ]
];

principalAxes = hold@With[
singularVectors = Transpose@#[[1]], #[[3]] &@
SingularValueDecomposition@matrix,
Map[Thick, Orange, Arrow@0, 0, # &, singularVectors[[1]]],
Map[Thick, Cyan, Arrow@0, 0, # &, singularVectors[[2]]]
];

additionalInfo = hold[
Column@
"PlotRange",
VerticalSlider[Dynamic@frameSize, 1, 10, 0.01,
Appearance -> "Labeled"]
, " ",
Column@
"Number of iterations",
VerticalSlider[Dynamic@numOfIterations, 1, 40, 1,
Appearance -> "Labeled"]

];

eigenvaluesDisplay = hold[
" ",
Dynamic@With[eigvals = Eigenvalues@matrix,
Graphics[Circle, Point@0, 0, Thick,
Arrow@0, 0, ReIm@eigvals[[1]],
Arrow@0, 0, ReIm@eigvals[[2]]
, Axes -> True, PlotRangePadding -> 0.1,
PlotRange -> -1, 1, -1, 1, ImageSize -> 200,
PlotLabel -> "Eigenvalues"]
]
];

arrowRepresentationActionMatrix[matrix_] :=
With[pts = MeshCoordinates@DiscretizeRegion@Region@Circle,
With[finalPts = Dot[matrix, #] & /@ pts,
Graphics[
PointSize@0.012, Point@finalPts,
Arrow /@ Thread@pts, finalPts
]
]];

Options[dynamicalEigenvectorsRepresentation] =
"ShowBluePointAndArrows" -> True,
"ShowEigenvectorsWhenReal" -> True,
"ShowEigenvalues" -> True,
"ShowPrincipalAxes" -> True
;
dynamicalEigenvectorsRepresentation[OptionsPattern] := DynamicModule[
v1 = 0.7, -0.6, v2 = 0.6, 0.6, v3 = 1, 1, movingPointIndex,
matrix, frameSize = 1.5, numOfIterations = 30,
Row[
EventHandler[
Dynamic[
matrix = Transpose@v1, v2;
Show[
arrowRepresentationActionMatrix@matrix,
Graphics[
PointSize@0.02, Circle, Point@0, 0,
"RedPoint", "GreenPoint", "BluePoint",
"ConditionallyShowEigenvectors",
"PrincipalAxes"
],
Frame -> True,
PlotRange -> Dynamic[-#, #, -#, # &@frameSize],
ImageSize -> 500
]
],
"MouseDown" :> With[mp = MousePosition["Graphics"],

movingPointIndex =
Position[v1, v2, v3, First@Nearest[v1, v2, v3, mp]][[1,
1]]
],
"MouseUp" :> (movingPointIndex = 0),
"MouseDragged" :> ReleaseHold[

Hold[Set][Hold[v1, v2, v3][[movingPointIndex]],
MousePosition["Graphics"]]
]
],
"AdditionalInfoSlot",
"EigenvaluesDisplay"
]
]~injectAndRelease~
"RedPoint" -> redPoint, "GreenPoint" -> greenPoint,
"BluePoint" ->
If[OptionValue@"ShowBluePointAndArrows" === True,
bluePointAndArrows, ],
"AdditionalInfoSlot" -> additionalInfo,
"EigenvaluesDisplay" ->
Sequence @@
If[OptionValue@"ShowEigenvalues" ===
True, eigenvaluesDisplay, ],
"ConditionallyShowEigenvectors" ->
If[OptionValue@"ShowEigenvectorsWhenReal" === True,
showEigenvectors, ],
"PrincipalAxes" ->
Sequence @@
If[OptionValue@"ShowPrincipalAxes" === True, principalAxes, ]
;

End;
EndPackage;


Then to create the representation just use



dynamicalEigenvectorsRepresentation[
"ShowEigenvectorsWhenReal" -> True,
"ShowBluePointAndArrows" -> True,
"ShowEigenvalues" -> True,
"ShowPrincipalAxes" -> False
]


and this is the result:



enter image description here






share|improve this answer


















  • 2




    This is really cool
    – user6014
    Nov 9 at 1:03












up vote
17
down vote










up vote
17
down vote









The following is an attempt to recreate a similar sort of interactive visualization, showing the eigenvectors (when real), and how the various points of the unit circle are transformed by the matrix.



The matrix can be chosen by moving its two column vectors using the mouse. I used EventHandler for this, instead of Locators, for greater customizability and a more natural look.



To ease code readability and modularity, the components of the graphical object are defined separately in a private context, and injected into the final DynamicModule object.



Here is the full code:



BeginPackage["eigenvectorRepresentation`"];
dynamicalEigenvectorsRepresentation;
Begin["`Private`"];

Attributes[hold] = HoldAllComplete;
ClearAll@injectAndRelease;
Attributes[injectAndRelease] = HoldAllComplete;
injectAndRelease[x_, replacementRules_, hold_: hold] :=
Hold@x /. replacementRules /. hold[s__] :> s // ReleaseHold;

redPoint = hold@
Red, If[TrueQ[movingPointIndex == 1], PointSize@0.04,
PointSize@0.03],
Point@v1, Arrow@0, 0, v1
;

greenPoint = hold@
Green,
If[TrueQ[movingPointIndex == 2], PointSize@0.04, PointSize@0.03],
Point@v2, Arrow@0, 0, v2
;

bluePointAndArrows =
hold@Dynamic@Blue, PointSize@0.03, Point@v3, Arrowheads@0.02,
Arrow /@
Partition[NestList[Dot[matrix, #] &, v3, numOfIterations], 2,
1]
;

showEigenvectors = hold@Dynamic@With[eigs = Eigenvectors@N@matrix,
If[MatchQ[eigs, __Real ..], Purple, Thickness@0.01,
InfiniteLine@-#, # & /@ eigs, ]
];

principalAxes = hold@With[
singularVectors = Transpose@#[[1]], #[[3]] &@
SingularValueDecomposition@matrix,
Map[Thick, Orange, Arrow@0, 0, # &, singularVectors[[1]]],
Map[Thick, Cyan, Arrow@0, 0, # &, singularVectors[[2]]]
];

additionalInfo = hold[
Column@
"PlotRange",
VerticalSlider[Dynamic@frameSize, 1, 10, 0.01,
Appearance -> "Labeled"]
, " ",
Column@
"Number of iterations",
VerticalSlider[Dynamic@numOfIterations, 1, 40, 1,
Appearance -> "Labeled"]

];

eigenvaluesDisplay = hold[
" ",
Dynamic@With[eigvals = Eigenvalues@matrix,
Graphics[Circle, Point@0, 0, Thick,
Arrow@0, 0, ReIm@eigvals[[1]],
Arrow@0, 0, ReIm@eigvals[[2]]
, Axes -> True, PlotRangePadding -> 0.1,
PlotRange -> -1, 1, -1, 1, ImageSize -> 200,
PlotLabel -> "Eigenvalues"]
]
];

arrowRepresentationActionMatrix[matrix_] :=
With[pts = MeshCoordinates@DiscretizeRegion@Region@Circle,
With[finalPts = Dot[matrix, #] & /@ pts,
Graphics[
PointSize@0.012, Point@finalPts,
Arrow /@ Thread@pts, finalPts
]
]];

Options[dynamicalEigenvectorsRepresentation] =
"ShowBluePointAndArrows" -> True,
"ShowEigenvectorsWhenReal" -> True,
"ShowEigenvalues" -> True,
"ShowPrincipalAxes" -> True
;
dynamicalEigenvectorsRepresentation[OptionsPattern] := DynamicModule[
v1 = 0.7, -0.6, v2 = 0.6, 0.6, v3 = 1, 1, movingPointIndex,
matrix, frameSize = 1.5, numOfIterations = 30,
Row[
EventHandler[
Dynamic[
matrix = Transpose@v1, v2;
Show[
arrowRepresentationActionMatrix@matrix,
Graphics[
PointSize@0.02, Circle, Point@0, 0,
"RedPoint", "GreenPoint", "BluePoint",
"ConditionallyShowEigenvectors",
"PrincipalAxes"
],
Frame -> True,
PlotRange -> Dynamic[-#, #, -#, # &@frameSize],
ImageSize -> 500
]
],
"MouseDown" :> With[mp = MousePosition["Graphics"],

movingPointIndex =
Position[v1, v2, v3, First@Nearest[v1, v2, v3, mp]][[1,
1]]
],
"MouseUp" :> (movingPointIndex = 0),
"MouseDragged" :> ReleaseHold[

Hold[Set][Hold[v1, v2, v3][[movingPointIndex]],
MousePosition["Graphics"]]
]
],
"AdditionalInfoSlot",
"EigenvaluesDisplay"
]
]~injectAndRelease~
"RedPoint" -> redPoint, "GreenPoint" -> greenPoint,
"BluePoint" ->
If[OptionValue@"ShowBluePointAndArrows" === True,
bluePointAndArrows, ],
"AdditionalInfoSlot" -> additionalInfo,
"EigenvaluesDisplay" ->
Sequence @@
If[OptionValue@"ShowEigenvalues" ===
True, eigenvaluesDisplay, ],
"ConditionallyShowEigenvectors" ->
If[OptionValue@"ShowEigenvectorsWhenReal" === True,
showEigenvectors, ],
"PrincipalAxes" ->
Sequence @@
If[OptionValue@"ShowPrincipalAxes" === True, principalAxes, ]
;

End;
EndPackage;


Then to create the representation just use



dynamicalEigenvectorsRepresentation[
"ShowEigenvectorsWhenReal" -> True,
"ShowBluePointAndArrows" -> True,
"ShowEigenvalues" -> True,
"ShowPrincipalAxes" -> False
]


and this is the result:



enter image description here






share|improve this answer














The following is an attempt to recreate a similar sort of interactive visualization, showing the eigenvectors (when real), and how the various points of the unit circle are transformed by the matrix.



The matrix can be chosen by moving its two column vectors using the mouse. I used EventHandler for this, instead of Locators, for greater customizability and a more natural look.



To ease code readability and modularity, the components of the graphical object are defined separately in a private context, and injected into the final DynamicModule object.



Here is the full code:



BeginPackage["eigenvectorRepresentation`"];
dynamicalEigenvectorsRepresentation;
Begin["`Private`"];

Attributes[hold] = HoldAllComplete;
ClearAll@injectAndRelease;
Attributes[injectAndRelease] = HoldAllComplete;
injectAndRelease[x_, replacementRules_, hold_: hold] :=
Hold@x /. replacementRules /. hold[s__] :> s // ReleaseHold;

redPoint = hold@
Red, If[TrueQ[movingPointIndex == 1], PointSize@0.04,
PointSize@0.03],
Point@v1, Arrow@0, 0, v1
;

greenPoint = hold@
Green,
If[TrueQ[movingPointIndex == 2], PointSize@0.04, PointSize@0.03],
Point@v2, Arrow@0, 0, v2
;

bluePointAndArrows =
hold@Dynamic@Blue, PointSize@0.03, Point@v3, Arrowheads@0.02,
Arrow /@
Partition[NestList[Dot[matrix, #] &, v3, numOfIterations], 2,
1]
;

showEigenvectors = hold@Dynamic@With[eigs = Eigenvectors@N@matrix,
If[MatchQ[eigs, __Real ..], Purple, Thickness@0.01,
InfiniteLine@-#, # & /@ eigs, ]
];

principalAxes = hold@With[
singularVectors = Transpose@#[[1]], #[[3]] &@
SingularValueDecomposition@matrix,
Map[Thick, Orange, Arrow@0, 0, # &, singularVectors[[1]]],
Map[Thick, Cyan, Arrow@0, 0, # &, singularVectors[[2]]]
];

additionalInfo = hold[
Column@
"PlotRange",
VerticalSlider[Dynamic@frameSize, 1, 10, 0.01,
Appearance -> "Labeled"]
, " ",
Column@
"Number of iterations",
VerticalSlider[Dynamic@numOfIterations, 1, 40, 1,
Appearance -> "Labeled"]

];

eigenvaluesDisplay = hold[
" ",
Dynamic@With[eigvals = Eigenvalues@matrix,
Graphics[Circle, Point@0, 0, Thick,
Arrow@0, 0, ReIm@eigvals[[1]],
Arrow@0, 0, ReIm@eigvals[[2]]
, Axes -> True, PlotRangePadding -> 0.1,
PlotRange -> -1, 1, -1, 1, ImageSize -> 200,
PlotLabel -> "Eigenvalues"]
]
];

arrowRepresentationActionMatrix[matrix_] :=
With[pts = MeshCoordinates@DiscretizeRegion@Region@Circle,
With[finalPts = Dot[matrix, #] & /@ pts,
Graphics[
PointSize@0.012, Point@finalPts,
Arrow /@ Thread@pts, finalPts
]
]];

Options[dynamicalEigenvectorsRepresentation] =
"ShowBluePointAndArrows" -> True,
"ShowEigenvectorsWhenReal" -> True,
"ShowEigenvalues" -> True,
"ShowPrincipalAxes" -> True
;
dynamicalEigenvectorsRepresentation[OptionsPattern] := DynamicModule[
v1 = 0.7, -0.6, v2 = 0.6, 0.6, v3 = 1, 1, movingPointIndex,
matrix, frameSize = 1.5, numOfIterations = 30,
Row[
EventHandler[
Dynamic[
matrix = Transpose@v1, v2;
Show[
arrowRepresentationActionMatrix@matrix,
Graphics[
PointSize@0.02, Circle, Point@0, 0,
"RedPoint", "GreenPoint", "BluePoint",
"ConditionallyShowEigenvectors",
"PrincipalAxes"
],
Frame -> True,
PlotRange -> Dynamic[-#, #, -#, # &@frameSize],
ImageSize -> 500
]
],
"MouseDown" :> With[mp = MousePosition["Graphics"],

movingPointIndex =
Position[v1, v2, v3, First@Nearest[v1, v2, v3, mp]][[1,
1]]
],
"MouseUp" :> (movingPointIndex = 0),
"MouseDragged" :> ReleaseHold[

Hold[Set][Hold[v1, v2, v3][[movingPointIndex]],
MousePosition["Graphics"]]
]
],
"AdditionalInfoSlot",
"EigenvaluesDisplay"
]
]~injectAndRelease~
"RedPoint" -> redPoint, "GreenPoint" -> greenPoint,
"BluePoint" ->
If[OptionValue@"ShowBluePointAndArrows" === True,
bluePointAndArrows, ],
"AdditionalInfoSlot" -> additionalInfo,
"EigenvaluesDisplay" ->
Sequence @@
If[OptionValue@"ShowEigenvalues" ===
True, eigenvaluesDisplay, ],
"ConditionallyShowEigenvectors" ->
If[OptionValue@"ShowEigenvectorsWhenReal" === True,
showEigenvectors, ],
"PrincipalAxes" ->
Sequence @@
If[OptionValue@"ShowPrincipalAxes" === True, principalAxes, ]
;

End;
EndPackage;


Then to create the representation just use



dynamicalEigenvectorsRepresentation[
"ShowEigenvectorsWhenReal" -> True,
"ShowBluePointAndArrows" -> True,
"ShowEigenvalues" -> True,
"ShowPrincipalAxes" -> False
]


and this is the result:



enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 13:16

























answered Nov 9 at 0:47









glS

4,91911142




4,91911142







  • 2




    This is really cool
    – user6014
    Nov 9 at 1:03












  • 2




    This is really cool
    – user6014
    Nov 9 at 1:03







2




2




This is really cool
– user6014
Nov 9 at 1:03




This is really cool
– user6014
Nov 9 at 1:03

















draft saved

draft discarded
















































Thanks for contributing an answer to Mathematica Stack Exchange!


  • 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.

Use MathJax to format equations. MathJax reference.


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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f185644%2fmaking-an-interactive-visualization-of-the-eigenvectors-of-two-dimensional-matri%23new-answer', 'question_page');

);

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







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ