.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/sparse_vector_normals.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 .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_sparse_vector_normals.py: A sparse model vector generator ================================= Demonstrates how to create sparse model vectors with small number of non-zero entries sampled from Gaussian distribution .. GENERATED FROM PYTHON SOURCE LINES 10-11 Let's import necessary libraries .. GENERATED FROM PYTHON SOURCE LINES 11-22 .. code-block:: default import matplotlib as mpl import matplotlib.pyplot as plt from jax import random import jax.numpy as jnp import cr.sparse as crs import cr.sparse.data as crdata from cr.nimble.dsp import ( nonzero_indices, nonzero_values ) .. GENERATED FROM PYTHON SOURCE LINES 23-24 Let's define the size of model and number of sparse entries .. GENERATED FROM PYTHON SOURCE LINES 24-30 .. code-block:: default # Model size N = 1000 # Number of non-zero entries in the sparse model K = 30 .. GENERATED FROM PYTHON SOURCE LINES 31-32 Let's generate a random sparse model .. GENERATED FROM PYTHON SOURCE LINES 32-36 .. code-block:: default key = random.PRNGKey(1) x, omega = crdata.sparse_normal_representations(key, N, K, 1) x = jnp.squeeze(x) .. GENERATED FROM PYTHON SOURCE LINES 37-38 We can easily find the locations of non-zero entries .. GENERATED FROM PYTHON SOURCE LINES 38-40 .. code-block:: default print(nonzero_indices(x)) .. rst-class:: sphx-glr-script-out .. code-block:: none [ 45 69 114 133 159 185 199 233 259 338 348 377 393 425 490 504 521 533 537 627 722 730 782 790 817 846 891 947 949 968] .. GENERATED FROM PYTHON SOURCE LINES 41-42 We can extract corresponding non-zero values in a compact vector .. GENERATED FROM PYTHON SOURCE LINES 42-44 .. code-block:: default print(nonzero_values(x)) .. rst-class:: sphx-glr-script-out .. code-block:: none [-0.64617896 -1.2449833 -0.9160154 2.7065587 0.99159986 1.7746289 0.8708357 -0.28911775 0.30582124 -1.0570332 -0.03854743 -2.4557247 -0.04477294 -2.2054574 -1.835958 1.8202777 -0.3686923 -0.54682994 -0.82268345 1.5236915 0.24205726 -0.01583949 1.150074 -0.00814029 -0.6977474 -0.88545537 0.4539182 1.7888712 -0.24443631 -0.39677632] .. GENERATED FROM PYTHON SOURCE LINES 45-46 Let's plot the vector to see where the non-zero entries are .. GENERATED FROM PYTHON SOURCE LINES 46-48 .. code-block:: default plt.figure(figsize=(8,6), dpi= 100, facecolor='w', edgecolor='k') plt.stem(x, markerfmt='.'); .. image-sg:: /gallery/images/sphx_glr_sparse_vector_normals_001.png :alt: sparse vector normals :srcset: /gallery/images/sphx_glr_sparse_vector_normals_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.457 seconds) .. _sphx_glr_download_gallery_sparse_vector_normals.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: sparse_vector_normals.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: sparse_vector_normals.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_