-
Notifications
You must be signed in to change notification settings - Fork 14
/
example2.m
68 lines (62 loc) · 1.8 KB
/
example2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
function [f,g] = example2(x,Data)
%EXAMPLE2 Matrix factorization.
% Compute Frobenius norm residual of an approximate
% (rank-reduced) two-factor decomposition of a matrix:
%
% 1/2 * ( || A - U*V' ||_F )^2
%
% Derivatives are computed using the matrix form.
%
% Input
% Data.A: matrix being approximated, A
% Data.rank: rank of approximation factors
% x: approximation encoded as follows:
% U = reshape(x(1:m*k),m,k);
% V = reshape(x(m*k+1:m*k+n*k),n,k);
%
% Output
% f: function value (residual)
% g: first derivatives of f w.r.t. x (i.e., U & V)
%
% Matrix dimensions
% A is m x n
% U is m x k
% V is n x k
%
% Examples
% % Defaults for example2_init: m = 10; n = 8; k = 6;
% [x0,Data] = example2_init;
% [f,g] = example2(x0,Data)
%
% % Larger problem
% m = 100; n = 80; k = 10;
% [x0,Data] = example2_init(m,n,k);
% out = ncg(@(x) example2(x,Data), x0)
%
% % Increase amount of computation allowed
% params = out.Params.Results;
% params.MaxIters = 1000;
% params.MaxFuncEvals = 2000;
% out = ncg(@(x) example2(x,Data), x0, params)
%
%Poblano Toolbox for MATLAB
%
%Copyright 2009 National Technology & Engineering Solutions of Sandia,
%LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the
%U.S. Government retains certain rights in this software.
% Notes:
% - Derivatives are computed using matrix form.
%
%% Data setup
% Data.A should have matrix being modeled
[m,n] = size(Data.A);
k = Data.rank;
U = reshape(x(1:m*k),m,k);
V = reshape(x(m*k+1:m*k+n*k),n,k);
%% Function value (residual)
AmUVt = Data.A-U*V';
f = 0.5*norm(AmUVt,'fro')^2;
%% First derivatives computed in matrix form
g = zeros((m+n)*k,1);
g(1:m*k) = -reshape(AmUVt*V,m*k,1);
g(m*k+1:end) = -reshape(AmUVt'*U,n*k,1);