Line data Source code
1 : /*
2 : * File: QuadraticOperator.cpp
3 : * Author: Pantelis Sopasakis
4 : *
5 : * Created on July 24, 2015, 8:49 PM
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 "QuadraticOperator.h"
22 : #include "ForBESUtils.h"
23 :
24 1 : QuadraticOperator::QuadraticOperator(LinearOperator& T) :
25 1 : Function(), m_T(T) {
26 1 : if (T.dimensionIn() != T.dimensionOut()) {
27 0 : throw std::invalid_argument("T has incompatible dimensions");
28 : }
29 1 : }
30 :
31 2 : QuadraticOperator::~QuadraticOperator() {
32 2 : }
33 :
34 :
35 2 : int QuadraticOperator::call(Matrix& x, double& f, Matrix& grad) {
36 2 : int status = computeGradient(x, grad);
37 2 : f = (x * grad).get(0, 0)/2;
38 2 : return status;
39 : }
40 :
41 1 : int QuadraticOperator::call(Matrix& x, double& f) {
42 1 : Matrix grad;
43 1 : int status = call(x, f, grad);
44 1 : return status;
45 : }
46 :
47 2 : int QuadraticOperator::computeGradient(Matrix& x, Matrix& grad) {
48 2 : grad = m_T.call(x); /* Tx = T[x]; assertion: Tx is a vector of the same length as x */
49 :
50 2 : if (grad.isEmpty()) {
51 0 : throw std::logic_error("Linear operator returned an empty vector");
52 : }
53 2 : if (!grad.isColumnVector()) {
54 0 : throw std::logic_error("Linear operator did not return a column vector");
55 : }
56 2 : if (grad.getNrows() != x.getNrows()) {
57 0 : throw std::logic_error("Incompatible dimensions between T(x) and x");
58 : }
59 2 : return ForBESUtils::STATUS_OK;
60 : }
61 :
62 0 : FunctionOntologicalClass QuadraticOperator::category() {
63 0 : return FunctionOntologyRegistry::quadratic();
64 3 : }
65 :
|