Line data Source code
1 : /*
2 : * File: ConjugateFunction.cpp
3 : * Author: Pantelis Sopasakis
4 : *
5 : * Created on November 7, 2015, 3:15 AM
6 : *
7 : * ForBES is free software: you can redistribute it and/or modify
8 : * it under the terms of the GNU Lesser General Public License as published by
9 : * the Free Software Foundation, either version 3 of the License, or
10 : * (at your option) any later version.
11 : *
12 : * ForBES is distributed in the hope that it will be useful,
13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : * GNU Lesser General Public License for more details.
16 : *
17 : * You should have received a copy of the GNU Lesser General Public License
18 : * along with ForBES. If not, see <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : #include "ConjugateFunction.h"
22 :
23 8 : ConjugateFunction::ConjugateFunction(Function& funct) :
24 8 : Function(), m_function(funct) {
25 8 : }
26 :
27 12 : ConjugateFunction::~ConjugateFunction() {
28 12 : }
29 :
30 1 : int ConjugateFunction::call(Matrix& x, double& f) {
31 1 : return m_function.callConj(x, f);
32 : }
33 :
34 1 : int ConjugateFunction::call(Matrix& x, double& f, Matrix& grad) {
35 1 : return m_function.callConj(x, f, grad);
36 : }
37 :
38 1 : int ConjugateFunction::callConj(Matrix& x, double& f_star, Matrix& grad) {
39 1 : return m_function.call(x, f_star, grad);
40 : }
41 :
42 1 : int ConjugateFunction::callConj(Matrix& x, double& f_star) {
43 1 : return m_function.call(x, f_star);
44 : }
45 :
46 1 : int ConjugateFunction::callProx(Matrix& x, double gamma, Matrix& prox) {
47 1 : double one_over_gamma = 1.0 / gamma;
48 1 : Matrix x_over_gamma = x;
49 1 : x_over_gamma *= one_over_gamma;
50 1 : int status = m_function.callProx(x_over_gamma, one_over_gamma, prox);
51 1 : prox *= gamma;
52 10 : for (size_t i = 0; i < x.getNrows(); i++) {
53 9 : prox.set(i, 0, x.get(i, 0) - prox.get(i, 0));
54 : }
55 1 : return status;
56 : }
57 :
58 0 : int ConjugateFunction::callProx(Matrix& x, double gamma, Matrix& prox, double& f_at_prox) {
59 0 : int status = callProx(x, gamma, prox);
60 0 : if (ForBESUtils::STATUS_OK != status) {
61 0 : return status;
62 : }
63 0 : return call(prox, f_at_prox);
64 : }
65 :
66 9 : FunctionOntologicalClass ConjugateFunction::category() {
67 9 : FunctionOntologicalClass meta("Conjugate");
68 18 : FunctionOntologicalClass orig_meta = m_function.category();
69 :
70 9 : if (orig_meta.is_quadratic()){
71 3 : FunctionOntologicalClass conj_quad_meta(FunctionOntologyRegistry::conj_quadratic().getName());
72 3 : meta.add_superclass(conj_quad_meta);
73 : }
74 :
75 9 : if (orig_meta.is_conjugate_quadratic()){
76 3 : FunctionOntologicalClass quad_meta(FunctionOntologyRegistry::quadratic().getName());
77 3 : meta.add_superclass(quad_meta);
78 : }
79 :
80 9 : meta.set_defines_f(orig_meta.defines_conjugate());
81 9 : meta.set_defines_grad(orig_meta.defines_conjugate_grad());
82 9 : meta.set_defines_conjugate(orig_meta.defines_f());
83 9 : meta.set_defines_conjugate_grad(orig_meta.defines_grad());
84 9 : meta.set_defines_prox(orig_meta.defines_prox());
85 18 : return meta;
86 3 : }
87 :
88 :
89 :
90 :
|