| Hosted by CoCalc | Download
%sage x = var('x') @interact def _(k = (1..10)): print(k) plot(x * sin(x), (x, -k*pi, k*pi)) print("k = %s" % k)
Interact: please open in CoCalc
%sage k = 2 plot(x * sin(x), (x, -k*pi, k*pi))
%r attach(faithful) hist(eruptions, seq(1.6, 5.2, 0.2), prob=TRUE) lines(density(eruptions, bw=0.1)) rug(eruptions)
@interact def demof(a = slider(-5, 5, .01, 4)): x = var("x") plot(sin(a*x), (-pi, 2*pi)).show()
Interact: please open in CoCalc
import numpy as np from scipy import misc I = misc.lena() E = np.asarray(I) U,s,V=np.linalg.svd(E)
N = 75 # U.shape[0] graphics_array([matrix_plot(U[:,:N]), matrix_plot(np.diag(s[:N])), matrix_plot(V[:N,:].T)])
@interact def svd_demo(N=slider(0, E.shape[0], 1, 50, 15)): reconstr = U[:,:N].dot(np.diag(s[:N]).dot(V[:N,:])) show(graphics_array([matrix_plot(I), matrix_plot(reconstr)]))
Interact: please open in CoCalc
svd_demo(N = 256)
unknown message type 'obj'
svd_demo(N = 100)
unknown message type 'obj'
svd_demo(N = 50)
unknown message type 'obj'
svd_demo(N = 32)
unknown message type 'obj'
svd_demo(N = 15)
unknown message type 'obj'
svd_demo(N = 10)
unknown message type 'obj'
svd_demo(N = 5)
unknown message type 'obj'
svd_demo(N = 1)
unknown message type 'obj'
svd_demo(N = 0)
unknown message type 'obj'
import numpy as np U, s, V = np.linalg.svd(E) U.shape, s.shape, V.shape
((512, 512), (512,), (512, 512))
N = 50 U[:,:N].dot(np.diag(s[:N]).dot(V[:,:N].T))
array([[ 62.17166002, -49.14283225, 55.58897907, ..., -38.44414064, -12.96930387, 27.74346039], [ 62.17166002, -49.14283225, 55.58897907, ..., -38.44414064, -12.96930387, 27.74346039], [ 62.17166002, -49.14283225, 55.58897907, ..., -38.44414064, -12.96930387, 27.74346039], ..., [ 102.9419529 , -115.29743923, -38.9206326 , ..., 33.99282739, 15.88387515, 16.2630756 ], [ 102.06058 , -116.69180488, -36.45098782, ..., 38.87899566, 22.05276646, 14.78230945], [ 102.06058 , -116.69180488, -36.45098782, ..., 38.87899566, 22.05276646, 14.78230945]])
np.linalg.svd?
File: /usr/local/sage/sage-6.5/local/lib/python2.7/site-packages/numpy/linalg/linalg.py Signature : np.linalg.svd(a, full_matrices=1, compute_uv=1) Docstring : Singular Value Decomposition. Factors the matrix a as "u * np.diag(s) * v", where u and v are unitary and s is a 1-d array of a's singular values. a : (..., M, N) array_like A real or complex matrix of shape (M, N) . full_matrices : bool, optional If True (default), u and v have the shapes (M, M) and (N, N), respectively. Otherwise, the shapes are (M, K) and (K, N), respectively, where K = min(M, N). compute_uv : bool, optional Whether or not to compute u and v in addition to s. True by default. u : { (..., M, M), (..., M, K) } array Unitary matrices. The actual shape depends on the value of "full_matrices". Only returned when "compute_uv" is True. s : (..., K) array The singular values for every matrix, sorted in descending order. v : { (..., N, N), (..., K, N) } array Unitary matrices. The actual shape depends on the value of "full_matrices". Only returned when "compute_uv" is True. LinAlgError If SVD computation does not converge. Broadcasting rules apply, see the numpy.linalg documentation for details. The decomposition is performed using LAPACK routine _gesdd The SVD is commonly written as "a = U S V.H". The v returned by this function is "V.H" and "u = U". If "U" is a unitary matrix, it means that it satisfies "U.H = inv(U)". The rows of v are the eigenvectors of "a.H a". The columns of u are the eigenvectors of "a a.H". For row "i" in v and column "i" in u, the corresponding eigenvalue is "s[i]**2". If a is a matrix object (as opposed to an ndarray), then so are all the return values. >>> a = np.random.randn(9, 6) + 1j*np.random.randn(9, 6) Reconstruction based on full SVD: >>> U, s, V = np.linalg.svd(a, full_matrices=True) >>> U.shape, V.shape, s.shape ((9, 9), (6, 6), (6,)) >>> S = np.zeros((9, 6), dtype=complex) >>> S[:6, :6] = np.diag(s) >>> np.allclose(a, np.dot(U, np.dot(S, V))) True Reconstruction based on reduced SVD: >>> U, s, V = np.linalg.svd(a, full_matrices=False) >>> U.shape, V.shape, s.shape ((9, 6), (6, 6), (6,)) >>> S = np.diag(s) >>> np.allclose(a, np.dot(U, np.dot(S, V))) True
E2 = matrix(E) E3 = E2.change_ring(RDF)
type(E) E.dtype E2.parent()
<type 'numpy.ndarray'> dtype('int64') Full MatrixSpace of 512 by 512 dense matrices over Integer Ring
E3.parent()
Full MatrixSpace of 512 by 512 dense matrices over Real Double Field
type(E3.numpy())
<type 'numpy.ndarray'>