GCC Code Coverage Report


Directory: ./
File: src/2geom/numeric/matrix.cpp
Date: 2024-03-18 17:01:34
Exec Total Coverage
Lines: 0 58 0.0%
Functions: 0 5 0.0%
Branches: 0 90 0.0%

Line Branch Exec Source
1 /*
2 * Matrix, MatrixView, ConstMatrixView classes wrap the gsl matrix routines;
3 * "views" mimic the semantic of C++ references: any operation performed
4 * on a "view" is actually performed on the "viewed object"
5 *
6 * Authors:
7 * Marco Cecchetti <mrcekets at gmail.com>
8 *
9 * Copyright 2008 authors
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it either under the terms of the GNU Lesser General Public
13 * License version 2.1 as published by the Free Software Foundation
14 * (the "LGPL") or, at your option, under the terms of the Mozilla
15 * Public License Version 1.1 (the "MPL"). If you do not alter this
16 * notice, a recipient may use your version of this file under either
17 * the MPL or the LGPL.
18 *
19 * You should have received a copy of the LGPL along with this library
20 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * You should have received a copy of the MPL along with this library
23 * in the file COPYING-MPL-1.1
24 *
25 * The contents of this file are subject to the Mozilla Public License
26 * Version 1.1 (the "License"); you may not use this file except in
27 * compliance with the License. You may obtain a copy of the License at
28 * http://www.mozilla.org/MPL/
29 *
30 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
31 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
32 * the specific language governing rights and limitations.
33 */
34
35
36 #include <2geom/numeric/matrix.h>
37 #include <2geom/numeric/vector.h>
38
39
40 namespace Geom { namespace NL {
41
42 Vector operator*( detail::BaseMatrixImpl const& A,
43 detail::BaseVectorImpl const& v )
44 {
45 assert(A.columns() == v.size());
46
47 Vector result(A.rows(), 0.0);
48 for (size_t i = 0; i < A.rows(); ++i)
49 for (size_t j = 0; j < A.columns(); ++j)
50 result[i] += A(i,j) * v[j];
51
52 return result;
53 }
54
55 Matrix operator*( detail::BaseMatrixImpl const& A,
56 detail::BaseMatrixImpl const& B )
57 {
58 assert(A.columns() == B.rows());
59
60 Matrix C(A.rows(), B.columns(), 0.0);
61 for (size_t i = 0; i < C.rows(); ++i)
62 for (size_t j = 0; j < C.columns(); ++j)
63 for (size_t k = 0; k < A.columns(); ++k)
64 C(i,j) += A(i,k) * B(k, j);
65
66 return C;
67 }
68
69 Matrix pseudo_inverse(detail::BaseMatrixImpl const& A)
70 {
71
72 Matrix U(A);
73 Matrix V(A.columns(), A.columns());
74 Vector s(A.columns());
75 gsl_vector* work = gsl_vector_alloc(A.columns());
76
77 gsl_linalg_SV_decomp( U.get_gsl_matrix(),
78 V.get_gsl_matrix(),
79 s.get_gsl_vector(),
80 work );
81
82 Matrix P(A.columns(), A.rows(), 0.0);
83
84 int sz = s.size();
85 while ( sz-- > 0 && s[sz] == 0 ) {}
86 ++sz;
87 if (sz == 0) return P;
88 VectorView sv(s, sz);
89
90 for (size_t i = 0; i < sv.size(); ++i)
91 {
92 VectorView v = V.column_view(i);
93 v.scale(1/sv[i]);
94 for (size_t h = 0; h < P.rows(); ++h)
95 for (size_t k = 0; k < P.columns(); ++k)
96 P(h,k) += V(h,i) * U(k,i);
97 }
98
99 return P;
100 }
101
102
103 double trace (detail::BaseMatrixImpl const& A)
104 {
105 if (A.rows() != A.columns())
106 {
107 THROW_RANGEERROR ("NL::Matrix: computing trace: "
108 "rows() != columns()");
109 }
110 double t = 0;
111 for (size_t i = 0; i < A.rows(); ++i)
112 {
113 t += A(i,i);
114 }
115 return t;
116 }
117
118
119 double det (detail::BaseMatrixImpl const& A)
120 {
121 if (A.rows() != A.columns())
122 {
123 THROW_RANGEERROR ("NL::Matrix: computing determinant: "
124 "rows() != columns()");
125 }
126
127 Matrix LU(A);
128 int s;
129 gsl_permutation * p = gsl_permutation_alloc(LU.rows());
130 gsl_linalg_LU_decomp (LU.get_gsl_matrix(), p, &s);
131
132 double t = 1;
133 for (size_t i = 0; i < LU.rows(); ++i)
134 {
135 t *= LU(i,i);
136 }
137
138 gsl_permutation_free(p);
139 return t;
140 }
141
142
143 } } // end namespaces
144
145 /*
146 Local Variables:
147 mode:c++
148 c-file-style:"stroustrup"
149 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
150 indent-tabs-mode:nil
151 fill-column:99
152 End:
153 */
154 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
155