source: tol/tol_pkg/BysVecLinReg/tools.tol @ ad7e8ca

1941-docker-image-to-testdevel
Last change on this file since ad7e8ca was ad7e8ca, checked in by Víctor de Buen <vdebuen@…>, 16 years ago

corregido error en vmatrix2vec.getPosition
refs #902

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@2155 be4846ae-f051-483a-831f-b64e7d08527f

  • Property mode set to 100644
File size: 3.6 KB
Line 
1/* -*- mode: C++ -*- */
2//////////////////////////////////////////////////////////////////////////////
3// FILE   : tools.tol
4// PURPOSE: Defines Class BysVecLinReg::@Multivariate
5//////////////////////////////////////////////////////////////////////////////
6
7//////////////////////////////////////////////////////////////////////////////
8//Converts M in a column vector of m*n rows with stacked columns of M having
9//m rows and n columns
10VMatrix vmatrix2vec(VMatrix M)
11//////////////////////////////////////////////////////////////////////////////
12{
13  Matrix Mt = VMat2Mat(M,True);
14  Mat2VMat(Tra(GetNumeric([[ Mt ]])))
15};
16
17//////////////////////////////////////////////////////////////////////////////
18//Converts a column vector into a tye
19VMatrix vec2vmatrix(VMatrix vec, VMatrix M)
20//////////////////////////////////////////////////////////////////////////////
21{
22  Matrix Mt = VMat2Mat(M,True);
23  Mat2VMat(DeepCopy([[Mt]],VMat2Mat(vec))[1],True)
24};
25
26//////////////////////////////////////////////////////////////////////////////
27//Returns the position of cell (i,j) of in stacked columns form of a matrix
28//with r rows and c columns
29Real vmatrix2vec.getPosition(Real i, Real j, Real r, Real c)
30//////////////////////////////////////////////////////////////////////////////
31{
32  MatDat(VMat2Triplet(vmatrix2vec(Triplet([[ [[i,j,1]] ]],r,c))),1,1)
33};
34
35//////////////////////////////////////////////////////////////////////////////
36//Draws an standard multinormal x with full rank constraining linear
37//equalities
38//
39//  e ~ N(0,I)
40//  C*e = c
41//
42// where
43//  e <: R^n
44//  C <: R^rxn, r<n, rank(C)=r
45//  c <: R^r
46//
47//ALGORITHM:
48//
49// 1. Get pivoting order p of columns of diagonal of C*C'
50// 2. Select p.1 as gaussian components of x as (n-r) last pivoted columns
51// 3. Select p.2 as dependent components of x as first r pivoted columns
52// 4. Draw e.1 as standard independent normal
53// 5. Rewrite constraining linear equalities conditioned to e.1
54//     
55//      C = (C.1 C.2)
56//      e' = (e.1' e.2')
57//      C.1 * e.1 + C.2 * e.2 = c
58//      c.2 = c - C.1*e.1
59//      C.2 * e.2 = c.2
60//
61// 6. Solve linear system to get e.2
62// 7. Checks if solution has the expected rounding error
63// 8. Merge e.1 and e.2 following the selected pivoting order to get the
64//    wanted draw of e
65//
66VMatrix eqConstrainedStdGaussian(VMatrix C, VMatrix c)
67//////////////////////////////////////////////////////////////////////////////
68{
69  Real r = VRows(C);
70  Real n = VColumns(C);
71  //Calculates diagonal of C*C' in a faster way
72  VMatrix C2 = Constant(1,r,1) * (C $* C);
73  //Builds pivoting order of columns of diagonal of C*C'
74  Matrix p = ColumnPivots(VMat2Mat(C2));
75  //Select p.1 as gaussian components of x as (n-r) last pivoted columns
76  Set p.1 = MatSet(Sub(p,1,r+1,1,n-r))[1];
77  //Select p.2 as dependent components of x as first r pivoted columns
78  Set p.2 = MatSet(Sub(p,1,1,  1,r  ))[1];
79  //Draw e.1 as standard independent normal
80  VMatrix e.1 = Gaussian(n-r,1,0,1);
81  //Rewrite constraining linear equalities conditioned to e.1
82  VMatrix C.1 = SubCol(C,p.1);
83  VMatrix C.2 = SubCol(C,p.2);
84  VMatrix c.2 = c-C.1 * e.1;
85  //Solve linear system to get e.2
86  VMatrix e.2 = MinimumResidualsSolve(C.2,c.2);
87  //Checking method success
88  VMatrix err = C.2*e.2-c.2;
89  Real errMax = VMatMax(Abs(err));
90  If((n*r^2)+errMax^2 != n*r^2,
91    WriteLn("[BysVecLinReg::eqConstrainedStdGaussian] errMax="<<errMax,"W"));
92  //Merging gaussian and dependent components following the selected
93  //pivoting order
94  Triplet(SetCol(p.1<<p.2) | Constant(n,1,1) | VMat2Mat(e.1 << e.2), n, 1)
95};
Note: See TracBrowser for help on using the repository browser.