.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/dimension_reduction/grassmann/plot_grassmann_karcher.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_dimension_reduction_grassmann_plot_grassmann_karcher.py: Karcher ================================== This example shows how to use the UQpy Grassmann class to use the logarithmic map and the exponential map .. GENERATED FROM PYTHON SOURCE LINES 10-12 Import the necessary libraries. Here we import standard libraries such as numpy and matplotlib, but also need to import the Grassmann class from UQpy implemented in the DimensionReduction module. .. GENERATED FROM PYTHON SOURCE LINES 15-22 .. code-block:: default import matplotlib.pyplot as plt import numpy as np from UQpy.dimension_reduction import GrassmannOperations from UQpy.dimension_reduction.grassmann_manifold.projections.SVDProjection import SVDProjection .. GENERATED FROM PYTHON SOURCE LINES 23-25 Generate four random matrices with reduced rank corresponding to the different samples. The samples are stored in `matrices`. .. GENERATED FROM PYTHON SOURCE LINES 28-57 .. code-block:: default D1 = 6 r0 = 2 # rank sample 0 r1 = 3 # rank sample 1 r2 = 4 # rank sample 2 r3 = 3 # rank sample 2 np.random.seed(1111) # For reproducibility. # Solutions: original space. Sol0 = np.dot(np.random.rand(D1, r0), np.random.rand(r0, D1)) Sol1 = np.dot(np.random.rand(D1, r1), np.random.rand(r1, D1)) Sol2 = np.dot(np.random.rand(D1, r2), np.random.rand(r2, D1)) Sol3 = np.dot(np.random.rand(D1, r3), np.random.rand(r3, D1)) # Creating a list of matrices. matrices = [Sol0, Sol1, Sol2, Sol3] # Plot the matrices fig, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4) ax1.title.set_text('Matrix 0') ax1.imshow(Sol0) ax2.title.set_text('Matrix 1') ax2.imshow(Sol1) ax3.title.set_text('Matrix 2') ax3.imshow(Sol2) ax4.title.set_text('Matrix 3') ax4.imshow(Sol3) plt.show() .. image-sg:: /auto_examples/dimension_reduction/grassmann/images/sphx_glr_plot_grassmann_karcher_001.png :alt: Matrix 0, Matrix 1, Matrix 2, Matrix 3 :srcset: /auto_examples/dimension_reduction/grassmann/images/sphx_glr_plot_grassmann_karcher_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 58-59 Instatiate the UQpy class Grassmann. .. GENERATED FROM PYTHON SOURCE LINES 62-77 .. code-block:: default manifold_projection = SVDProjection(matrices, p="max") # Plot the points on the Grassmann manifold defined by the left singular eigenvectors. fig, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4) ax1.title.set_text('Matrix 0') ax1.imshow(manifold_projection.u[0].data) ax2.title.set_text('Matrix 1') ax2.imshow(manifold_projection.u[0].data) ax3.title.set_text('Matrix 2') ax3.imshow(manifold_projection.u[0].data) ax4.title.set_text('Matrix 3') ax4.imshow(manifold_projection.u[0].data) plt.show() .. image-sg:: /auto_examples/dimension_reduction/grassmann/images/sphx_glr_plot_grassmann_karcher_002.png :alt: Matrix 0, Matrix 1, Matrix 2, Matrix 3 :srcset: /auto_examples/dimension_reduction/grassmann/images/sphx_glr_plot_grassmann_karcher_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 78-79 Compute the Karcher mean. .. GENERATED FROM PYTHON SOURCE LINES 82-93 .. code-block:: default from UQpy.utilities.distances.grassmannian_distances.GeodesicDistance import GeodesicDistance distance_method = GeodesicDistance() karcher_psi = GrassmannOperations.karcher_mean(grassmann_points=manifold_projection.u, distance=distance_method, optimization_method="GradientDescent") karcher_phi = GrassmannOperations.karcher_mean(grassmann_points=manifold_projection.v, distance=distance_method, optimization_method="GradientDescent") .. GENERATED FROM PYTHON SOURCE LINES 94-95 Project :math:`\Psi`, the left singular eigenvectors, on the tangent space centered at the Karcher mean. .. GENERATED FROM PYTHON SOURCE LINES 98-119 .. code-block:: default points_tangent = GrassmannOperations.log_map(grassmann_points=manifold_projection.u, reference_point=karcher_psi) print(points_tangent[0]) print(points_tangent[1]) print(points_tangent[2]) print(points_tangent[3]) # Plot the matrices fig, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4) ax1.title.set_text('Matrix 0') ax1.imshow(points_tangent[0]) ax2.title.set_text('Matrix 1') ax2.imshow(points_tangent[1]) ax3.title.set_text('Matrix 2') ax3.imshow(points_tangent[2]) ax4.title.set_text('Matrix 3') ax4.imshow(points_tangent[3]) plt.show() .. image-sg:: /auto_examples/dimension_reduction/grassmann/images/sphx_glr_plot_grassmann_karcher_003.png :alt: Matrix 0, Matrix 1, Matrix 2, Matrix 3 :srcset: /auto_examples/dimension_reduction/grassmann/images/sphx_glr_plot_grassmann_karcher_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none [[-0.00874178 0.01419143 -0.08720523 0.01862981] [-0.0916101 -0.10385387 -0.31331941 0.10120187] [ 0.1758779 0.25437567 0.4707719 -0.17381993] [ 0.02578807 0.01932748 0.11175552 -0.03217648] [-0.10366549 -0.042606 -0.53267726 0.14240933] [ 0.08736645 -0.04944982 0.65188218 -0.15179619]] [[-0.00164546 -0.03829288 0.02513578 -0.00281584] [-0.02348861 0.17276663 0.14955101 0.15385815] [ 0.04645434 -0.45667328 -0.26232557 -0.33530845] [ 0.00636704 -0.02611604 -0.04656453 -0.03611823] [-0.0247273 0.02523335 0.20300278 0.11971765] [ 0.01872903 0.17273658 -0.20956442 -0.03892621]] [[-0.00739245 -0.05418637 -0.01362178 -0.03505482] [ 0.06936717 -0.08088908 0.28755058 -0.03857011] [-0.16514472 0.04927188 -0.64574182 0.00246349] [-0.01376707 0.04187113 -0.06406643 0.02375415] [ 0.03494311 -0.23596817 0.19776162 -0.14116597] [ 0.02017418 0.3634352 -0.02124853 0.23008468]] [[ 0.01779289 0.07830828 0.07512116 0.01884436] [ 0.04552027 0.00987011 -0.124236 -0.21745632] [-0.05670579 0.1575746 0.43696485 0.50782605] [-0.0183423 -0.0345807 -0.00078028 0.04493767] [ 0.09331433 0.25164575 0.12976198 -0.12300034] [-0.12627377 -0.48607765 -0.41739094 -0.03656601]] .. GENERATED FROM PYTHON SOURCE LINES 120-121 Map the points back to the Grassmann manifold. .. GENERATED FROM PYTHON SOURCE LINES 124-138 .. code-block:: default points_grassmann = GrassmannOperations.exp_map(tangent_points=points_tangent, reference_point=manifold_projection.u[0]) # Plot the matrices fig, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4) ax1.title.set_text('Matrix 0') ax1.imshow(points_grassmann[0].data) ax2.title.set_text('Matrix 1') ax2.imshow(points_grassmann[1].data) ax3.title.set_text('Matrix 2') ax3.imshow(points_grassmann[2].data) ax4.title.set_text('Matrix 3') ax4.imshow(points_grassmann[3].data) plt.show() .. image-sg:: /auto_examples/dimension_reduction/grassmann/images/sphx_glr_plot_grassmann_karcher_004.png :alt: Matrix 0, Matrix 1, Matrix 2, Matrix 3 :srcset: /auto_examples/dimension_reduction/grassmann/images/sphx_glr_plot_grassmann_karcher_004.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.356 seconds) .. _sphx_glr_download_auto_examples_dimension_reduction_grassmann_plot_grassmann_karcher.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/SURGroup/UQpy/master?urlpath=lab/tree/notebooks/auto_examples/dimension_reduction/grassmann/plot_grassmann_karcher.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_grassmann_karcher.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_grassmann_karcher.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_