Cody

# Problem 3072. Singular Value Decomposition

Solution 2328608

Submitted on 22 May 2020
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
A = [3 0; 0 -2]; [U,S,V] = sing_val_decomp(A); assert(isequal(U,[1 0; 0 1])) assert(isequal(S,[3 0; 0 2])) assert(isequal(V,[1 0; 0 -1]))

U = 1 0 0 1 S = 3 0 0 2 V = 1 0 0 -1

2   Pass
A = [0 2; 0 0; 0 0]; [U,S,V] = sing_val_decomp(A); assert(isequal(U,eye(3))) assert(isequal(S,fliplr(A))) assert(isequal(V,[0 -1; 1 0]))

U = 1 0 0 0 1 0 0 0 1 S = 2 0 0 0 0 0 V = 0 -1 1 0

3   Fail
A = [3 1 1; -1 3 1]; [U,S,V] = sing_val_decomp(A); assert(sum(sum(abs(U-(-[1 1; 1 -1]/sqrt(2)))))<1e-5) assert(sum(sum(abs(S-[sqrt(12) 0 0; 0 sqrt(10) 0])))<1e-5) assert(sum(sum(abs(V-([-0.40824829*[1;2;1],0.8944*[-1;.5;0],0.1826*[-1;-2;5]]))))<5e-4)

U = -0.7071 0.7071 -0.7071 -0.7071 S = 3.4641 0 0 0 3.1623 0 V = -0.4082 0.8944 -0.1826 -0.8165 -0.4472 -0.3651 -0.4082 -0.0000 0.9129

Assertion failed.

4   Pass
A = magic(3); [U,S,V] = sing_val_decomp(A); assert(sum(sum(abs(U-[-0.577350*ones(3,1), 0.70710678*[1;0;-1], 0.40824829*[1;-2;1]])))<1e-5) assert(sum(sum(abs(S-[15,0,0;0,6.928203,0;0,0,3.4641016])))<1e-5) assert(sum(sum(abs(V-[-0.577350*ones(3,1), 0.40824829*[1;-2;1], 0.70710678*[1;0;-1]])))<1e-5)

U = -0.5774 0.7071 0.4082 -0.5774 0.0000 -0.8165 -0.5774 -0.7071 0.4082 S = 15.0000 0 0 0 6.9282 0 0 0 3.4641 V = -0.5774 0.4082 0.7071 -0.5774 -0.8165 -0.0000 -0.5774 0.4082 -0.7071

5   Pass
ind = randi(4); switch ind case 1 A = [3 0; 0 -2]; [U,S,V] = sing_val_decomp(A); assert(isequal(U,[1 0; 0 1])) assert(isequal(S,[3 0; 0 2])) assert(isequal(V,[1 0; 0 -1])) case 2 A = [0 2; 0 0; 0 0]; [U,S,V] = sing_val_decomp(A); assert(isequal(U,eye(3))) assert(isequal(S,fliplr(A))) assert(isequal(V,[0 -1; 1 0])) case 3 A = [3 1 1; -1 3 1]; [U,S,V] = sing_val_decomp(A); assert(sum(sum(abs(U-(-[1 1; 1 -1]/sqrt(2)))))<1e-5) assert(sum(sum(abs(S-[sqrt(12) 0 0; 0 sqrt(10) 0])))<1e-5) assert(sum(sum(abs(V-([-0.40824829*[1;2;1],0.8944*[-1;.5;0],0.1826*[-1;-2;5]]))))<5e-4) case 4 A = magic(3); [U,S,V] = sing_val_decomp(A); assert(sum(sum(abs(U-[-0.577350*ones(3,1), 0.70710678*[1;0;-1], 0.40824829*[1;-2;1]])))<1e-5) assert(sum(sum(abs(S-[15,0,0;0,6.928203,0;0,0,3.4641016])))<1e-5) assert(sum(sum(abs(V-[-0.577350*ones(3,1), 0.40824829*[1;-2;1], 0.70710678*[1;0;-1]])))<1e-5) end

U = -0.5774 0.7071 0.4082 -0.5774 0.0000 -0.8165 -0.5774 -0.7071 0.4082 S = 15.0000 0 0 0 6.9282 0 0 0 3.4641 V = -0.5774 0.4082 0.7071 -0.5774 -0.8165 -0.0000 -0.5774 0.4082 -0.7071

6   Pass
ind = randi(4); switch ind case 1 A = [3 0; 0 -2]; [U,S,V] = sing_val_decomp(A); assert(isequal(U,[1 0; 0 1])) assert(isequal(S,[3 0; 0 2])) assert(isequal(V,[1 0; 0 -1])) case 2 A = [0 2; 0 0; 0 0]; [U,S,V] = sing_val_decomp(A); assert(isequal(U,eye(3))) assert(isequal(S,fliplr(A))) assert(isequal(V,[0 -1; 1 0])) case 3 A = [3 1 1; -1 3 1]; [U,S,V] = sing_val_decomp(A); assert(sum(sum(abs(U-(-[1 1; 1 -1]/sqrt(2)))))<1e-5) assert(sum(sum(abs(S-[sqrt(12) 0 0; 0 sqrt(10) 0])))<1e-5) assert(sum(sum(abs(V-([-0.40824829*[1;2;1],0.8944*[-1;.5;0],0.1826*[-1;-2;5]]))))<5e-4) case 4 A = magic(3); [U,S,V] = sing_val_decomp(A); assert(sum(sum(abs(U-[-0.577350*ones(3,1), 0.70710678*[1;0;-1], 0.40824829*[1;-2;1]])))<1e-5) assert(sum(sum(abs(S-[15,0,0;0,6.928203,0;0,0,3.4641016])))<1e-5) assert(sum(sum(abs(V-[-0.577350*ones(3,1), 0.40824829*[1;-2;1], 0.70710678*[1;0;-1]])))<1e-5) end

U = -0.5774 0.7071 0.4082 -0.5774 0.0000 -0.8165 -0.5774 -0.7071 0.4082 S = 15.0000 0 0 0 6.9282 0 0 0 3.4641 V = -0.5774 0.4082 0.7071 -0.5774 -0.8165 -0.0000 -0.5774 0.4082 -0.7071

7   Fail
ind = randi(4); switch ind case 1 A = [3 0; 0 -2]; [U,S,V] = sing_val_decomp(A); assert(isequal(U,[1 0; 0 1])) assert(isequal(S,[3 0; 0 2])) assert(isequal(V,[1 0; 0 -1])) case 2 A = [0 2; 0 0; 0 0]; [U,S,V] = sing_val_decomp(A); assert(isequal(U,eye(3))) assert(isequal(S,fliplr(A))) assert(isequal(V,[0 -1; 1 0])) case 3 A = [3 1 1; -1 3 1]; [U,S,V] = sing_val_decomp(A); assert(sum(sum(abs(U-(-[1 1; 1 -1]/sqrt(2)))))<1e-5) assert(sum(sum(abs(S-[sqrt(12) 0 0; 0 sqrt(10) 0])))<1e-5) assert(sum(sum(abs(V-([-0.40824829*[1;2;1],0.8944*[-1;.5;0],0.1826*[-1;-2;5]]))))<5e-4) case 4 A = magic(3); [U,S,V] = sing_val_decomp(A); assert(sum(sum(abs(U-[-0.577350*ones(3,1), 0.70710678*[1;0;-1], 0.40824829*[1;-2;1]])))<1e-5) assert(sum(sum(abs(S-[15,0,0;0,6.928203,0;0,0,3.4641016])))<1e-5) assert(sum(sum(abs(V-[-0.577350*ones(3,1), 0.40824829*[1;-2;1], 0.70710678*[1;0;-1]])))<1e-5) end

U = -0.7071 0.7071 -0.7071 -0.7071 S = 3.4641 0 0 0 3.1623 0 V = -0.4082 0.8944 -0.1826 -0.8165 -0.4472 -0.3651 -0.4082 -0.0000 0.9129

Assertion failed.