Changeset e1355dd in tol for tol_pkg/MulVarFunAprox


Ignore:
Timestamp:
Nov 30, 2009, 10:56:24 AM (17 years ago)
Author:
Víctor de Buen <vdebuen@…>
Branches:
1941-docker-image-to-test, devel, master
Children:
ea3d3da
Parents:
9fbb134
Message:

Class @BaseC2FunRn2R is moved to package FunRn2R and renamed as @BaseC2

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tol_pkg/MulVarFunAprox/MulVarFunAprox.tol

    r9fbb134 re1355dd  
    88
    99#Require MatQuery;
     10#Require FunRn2R;
    1011
    1112Text _.autodoc.description = "Aproximation of multivariate real functions "
    … …  
    1415Set _.autodoc.keys = [["aproximation","interpolation"]];
    1516Set _.autodoc.authors = [[
    16    "vdebuen@tol-project.org"   ]];
    17 
    18 //////////////////////////////////////////////////////////////////////////////
    19 //Base of K mutivariate arbitrary linearly independent functions R^n -> R of
    20 //class C2, ie, continuous and having first and second partial continuous
    21 //derivatives
    22 //////////////////////////////////////////////////////////////////////////////
    23 Class @BaseC2FunRn2R
    24 {
    25   //Dimension of domain vectorial space
    26   Real _.n;
    27   //Dimension of the base
    28   Real _.K;
    29 
    30   //Evaluates the k-th basic function for a
    31   //point x given as a row matrix
    32   Real function.r(Real k, VMatrix x);
    33   //Evaluates the first partial derivative of k-th basic function for a 
    34   //point x given as a row matrix
    35   Real partial.derivative.1.r(Real k, VMatrix x, Real i);
    36   //Evaluates the second partial derivative of k-th basic function for a 
    37   //point x given as a row matrix
    38   Real partial.derivative.2.r(Real k, VMatrix x, Real i, Real j);
    39 
    40   //For efficience reasonsthe base has specific method for evaluation over
    41   //massive data stored in a matrix
    42 
    43   //Evaluates the k-th basic function for an
    44   //array of points given as the rows of a matrix
    45   VMatrix function.v(Real k, VMatrix X);
    46   //Evaluates the first partial derivative of k-th basic function for an 
    47   //array of points given as the rows of a matrix
    48   VMatrix partial.derivative.1.v(Real k, VMatrix X, Real i);
    49   //Evaluates the second partial derivative of k-th basic function for an 
    50   //array of points given as the rows of a matrix
    51   VMatrix partial.derivative.2.v(Real k, VMatrix X, Real i, Real j)
    52 };
    53 
    54 //////////////////////////////////////////////////////////////////////////////
    55 Class @BaseC2FunRn2R.Map : @BaseC2FunRn2R
    56 //////////////////////////////////////////////////////////////////////////////
    57 {
    58   @BaseC2FunRn2R _.base;
    59   Set _.map;
    60 
    61   Static Text _.autodoc.description = "Extracts a sub-base of a given base "
    62   "whose k-th basic function are given by _.map[k] basic function of "
    63   "_.base. Of course _.map cannot have repated indexes to ensure linear "
    64   "independence.";
    65 
    66   ////////////////////////////////////////////////////////////////////////////
    67   Static Text _.autodoc.member.New = "Creates an instance of "
    68   "@BaseC2FunRn2R.Map on R^n with specified maximum degree.";
    69   Static @BaseC2FunRn2R.Map New(@BaseC2FunRn2R base, Set map)
    70   ////////////////////////////////////////////////////////////////////////////
    71   {
    72     @BaseC2FunRn2R.Map aux = [[
    73       Real _.n = base::_.n;
    74       Real _.K = Card(map);
    75       @BaseC2FunRn2R _.base = base;
    76       Set _.map = map   
    77   ]]};
    78   ////////////////////////////////////////////////////////////////////////////
    79   Static Text _.autodoc.member.function.r =
    80   "Evaluates the k-th basic function for a point x given as a row matrix";
    81   Real function.r(Real k, VMatrix x)
    82   ////////////////////////////////////////////////////////////////////////////
    83   {
    84     _.base::function.r(_.map[k], x)
    85   };
    86   ////////////////////////////////////////////////////////////////////////////
    87   Static Text _.autodoc.member.partial.derivative.1.r =
    88   "Evaluates the first partial derivative of k-th basic function for a " 
    89   "point x given as a row matrix";
    90   Real partial.derivative.1.r(Real k, VMatrix x, Real i)
    91   ////////////////////////////////////////////////////////////////////////////
    92   {
    93     partial.derivative.1.r(_.map[k], x, i)
    94   };
    95   ////////////////////////////////////////////////////////////////////////////
    96   Static Text _.autodoc.member.partial.derivative.2.r =
    97   "Evaluates the second partial derivative of k-th basic function for a "
    98   "point x given as a row matrix";
    99   Real partial.derivative.2.r(Real k, VMatrix x, Real i, Real j)
    100   ////////////////////////////////////////////////////////////////////////////
    101   {
    102     _.base::partial.derivative.2.r(_.map[k], x, i, j)
    103   };
    104 
    105   //For efficience reasonsthe base has specific method for evaluation over
    106   //massive data stored in a matrix
    107 
    108   ////////////////////////////////////////////////////////////////////////////
    109   Static Text _.autodoc.member.function.v =
    110   "Evaluates the k-th basic function for an "
    111   "array of points given as the rows of a matrix ";
    112   VMatrix function.v(Real k, VMatrix X)
    113   ////////////////////////////////////////////////////////////////////////////
    114   {
    115     _.base::function.v(_.map[k], X)
    116   };
    117   ////////////////////////////////////////////////////////////////////////////
    118   Static Text _.autodoc.member.partial.derivative.1.v =
    119   "Evaluates the first partial derivative of k-th basic function for an "
    120   "array of points given as the rows of a matrix";
    121   VMatrix partial.derivative.1.v(Real k, VMatrix X, Real i)
    122   ////////////////////////////////////////////////////////////////////////////
    123   {
    124     _.base::partial.derivative.1.v(_.map[k], X, i)
    125   };
    126   ////////////////////////////////////////////////////////////////////////////
    127   Static Text _.autodoc.member.partial.derivative.2.v =
    128   "Evaluates the second partial derivative of k-th basic function for an "
    129   "array of points given as the rows of a matrix";
    130   VMatrix partial.derivative.2.v(Real k, VMatrix X, Real i, Real j)
    131   ////////////////////////////////////////////////////////////////////////////
    132   {
    133     _.base::partial.derivative.2.v(_.map[k], X, i, j)
    134   }
    135 };
    136 
    137 //////////////////////////////////////////////////////////////////////////////
    138 Class @BaseC2FunRn2R.Concat : @BaseC2FunRn2R
    139 //////////////////////////////////////////////////////////////////////////////
    140 {
    141   Static Text _.autodoc.description = "Concats a set of linearly independent "
    142   "bases over the same domain vectorial space.";
    143 
    144   Set _.base;   //Set of @BaseC2FunRn2R
    145 
    146   //Auxiliar member to map concatenated bases
    147   Set _.map;
    148 
    149   ////////////////////////////////////////////////////////////////////////////
    150   Static Text _.autodoc.member.New = "Creates an instance of "
    151   "@BaseC2FunRn2R.Concat on R^n with specified maximum degree.";
    152   Static @BaseC2FunRn2R.Concat New(Set base)
    153   ////////////////////////////////////////////////////////////////////////////
    154   {
    155     Real n = (base[1])::_.n;
    156     Set map = BinGroup("<<", For(1,Card(base),Set(Real h)
    157     {
    158       If((base[h])::_.n!=n,WriteLn("[@BaseC2FunRn2R.Concat::New] "
    159       "Cannot concat bases with different domain vectorial space dimension "
    160       <<n+"!="<<(base[h])::_.n),"E");
    161       For(1,(base[h])::_.K,Set(Real k)
    162       {
    163         [[h,k]]
    164       })
    165     }));
    166     @BaseC2FunRn2R.Concat aux = [[
    167       Real _.n = base::_.n;
    168       Real _.K = Card(map);
    169       Set _.base = base;
    170       Set _.map = map
    171   ]]};
    172   ////////////////////////////////////////////////////////////////////////////
    173   Static Text _.autodoc.member.function.r =
    174   "Evaluates the k-th basic function for a point x given as a row matrix";
    175   Real function.r(Real k, VMatrix x)
    176   ////////////////////////////////////////////////////////////////////////////
    177   {
    178     Set aux = _.map[k];
    179     (_.base[aux::h])::function.r(aux::k, x)
    180   };
    181   ////////////////////////////////////////////////////////////////////////////
    182   Static Text _.autodoc.member.partial.derivative.1.r =
    183   "Evaluates the first partial derivative of k-th basic function for a " 
    184   "point x given as a row matrix";
    185   Real partial.derivative.1.r(Real k, VMatrix x, Real i)
    186   ////////////////////////////////////////////////////////////////////////////
    187   {
    188     Set aux = _.map[k];
    189     (_.base[aux::h])::partial.derivative.1.r(aux::k, x, i)
    190   };
    191   ////////////////////////////////////////////////////////////////////////////
    192   Static Text _.autodoc.member.partial.derivative.2.r =
    193   "Evaluates the second partial derivative of k-th basic function for a "
    194   "point x given as a row matrix";
    195   Real partial.derivative.2.r(Real k, VMatrix x, Real i, Real j)
    196   ////////////////////////////////////////////////////////////////////////////
    197   {
    198     Set aux = _.map[k];
    199     (_.base[aux::h])::_.base::partial.derivative.2.r(aux::k, x, i, j)
    200   };
    201 
    202   //For efficience reasonsthe base has specific method for evaluation over
    203   //massive data stored in a matrix
    204 
    205   ////////////////////////////////////////////////////////////////////////////
    206   Static Text _.autodoc.member.function.v =
    207   "Evaluates the k-th basic function for an "
    208   "array of points given as the rows of a matrix ";
    209   VMatrix function.v(Real k, VMatrix X)
    210   ////////////////////////////////////////////////////////////////////////////
    211   {
    212     Set aux = _.map[k];
    213     (_.base[aux::h])::_.base::function.v(aux::k, X)
    214   };
    215   ////////////////////////////////////////////////////////////////////////////
    216   Static Text _.autodoc.member.partial.derivative.1.v =
    217   "Evaluates the first partial derivative of k-th basic function for an "
    218   "array of points given as the rows of a matrix";
    219   VMatrix partial.derivative.1.v(Real k, VMatrix X, Real i)
    220   ////////////////////////////////////////////////////////////////////////////
    221   {
    222     Set aux = _.map[k];
    223     (_.base[aux::h])::_.base::partial.derivative.1.v(aux::k, X, i)
    224   };
    225   ////////////////////////////////////////////////////////////////////////////
    226   Static Text _.autodoc.member.partial.derivative.2.v =
    227   "Evaluates the second partial derivative of k-th basic function for an "
    228   "array of points given as the rows of a matrix";
    229   VMatrix partial.derivative.2.v(Real k, VMatrix X, Real i, Real j)
    230   ////////////////////////////////////////////////////////////////////////////
    231   {
    232     Set aux = _.map[k];
    233     (_.base[aux::h])::_.base::partial.derivative.2.v(aux::k, X, i, j)
    234   }
    235 };
    236 
    237 //////////////////////////////////////////////////////////////////////////////
    238 Class @BaseC2FunRn2R.Monomial : @BaseC2FunRn2R
    239 //////////////////////////////////////////////////////////////////////////////
    240 {
    241   Static Text _.autodoc.description = "Base of K mutivariate monomial "
    242   "functions R^n -> R . Linear independence is ensured since distinct "
    243   "monomials are allways independent\n";
    244 
    245   //Maximum degree of the monomial: sum of degrees of all variables
    246   Real _.maxDeg;
    247 
    248   ////////////////////////////////////////////////////////////////////////////
    249   // Auxiliar memebers
    250   ////////////////////////////////////////////////////////////////////////////
    251 
    252   //Combination of degrees for each basic function
    253   Set  _.degComb;
    254   //Indexes for derivatives {1,...,n} - {i}
    255   Set  _.range.der.1;
    256   //Indexes for second derivatives {1,...,n} - {i,j}
    257   Set  _.range.der.2;
    258 
    259   ////////////////////////////////////////////////////////////////////////////
    260   Static Text _.autodoc.member.New = "Creates an instance of "
    261   "@BaseC2FunRn2R.Monomial on R^n with specified maximum degree.";
    262   Static @BaseC2FunRn2R.Monomial New(Real n, Real maxDeg)
    263   ////////////////////////////////////////////////////////////////////////////
    264   {
    265     VMatrix degCombAll = Mat2VMat(SetMat(Range(0,maxDeg,1)^n));
    266     VMatrix degSum = degCombAll*Rand(n,1,1,1);
    267     VMatrix match = LE(degSum,Rand(VRows(degCombAll),1,maxDeg,maxDeg));
    268     Set select = MatQuery::SelectMatch(match);
    269     Set degComb = VMat2Set(SubRow(degCombAll, select));
    270     Real K = Card(degComb);
    271     Set range.der.1 = For(1,n,Set(Real i)
    272     {
    273       Range(1,n,1)-[[i]]
    274     });
    275     Set range.der.2 = For(1,n,Set(Real i)
    276     {
    277       For(1,n,Set(Real j)
    278       {
    279         Range(1,n,1)-[[i,j]]
    280       })
    281     });
    282     @BaseC2FunRn2R.Monomial aux = [[
    283       Real _.n = n;
    284       Real _.K = K;
    285       Real _.maxDeg = maxDeg;
    286       Set _.degComb = degComb;
    287       Set _.range.der.1 = range.der.1;
    288       Set _.range.der.2 = range.der.2
    289   ]]};
    290 
    291   ////////////////////////////////////////////////////////////////////////////
    292   Static Text _.autodoc.member.function.r =
    293   "Evaluates the k-th basic function for a point x given as a row matrix";
    294   Real function.r(Real k, VMatrix x)
    295   ////////////////////////////////////////////////////////////////////////////
    296   {
    297     SetProd(For(1,_.n, Real(Real i)
    298     {
    299       MatDat(x,1,i)^_.degComb[k][i]
    300     }))
    301   };
    302   ////////////////////////////////////////////////////////////////////////////
    303   Static Text _.autodoc.member.partial.derivative.1.r =
    304   "Evaluates the first partial derivative of k-th basic function for a " 
    305   "point x given as a row matrix";
    306   Real partial.derivative.1.r(Real k, VMatrix x, Real i)
    307   ////////////////////////////////////////////////////////////////////////////
    308   {
    309     Real d = _.degComb[k][i];
    310     If(d==0, 0,
    311     {
    312       (MatDat(x,1,i)^(d-1))*d*
    313       SetProd(EvalSet(_.range.der.1[i], Real(Real h)
    314       {
    315         MatDat(x,1,h)^_.degComb[k][h]
    316       }))
    317     })
    318   };
    319   ////////////////////////////////////////////////////////////////////////////
    320   Static Text _.autodoc.member.partial.derivative.2.r =
    321   "Evaluates the second partial derivative of k-th basic function for a "
    322   "point x given as a row matrix";
    323   Real partial.derivative.2.r(Real k, VMatrix x, Real i, Real j)
    324   ////////////////////////////////////////////////////////////////////////////
    325   {
    326     If(i==j,
    327     {
    328       Real d = _.degComb[k][i];
    329       If(d<=1, 0,
    330       {
    331         (MatDat(x,1,i)^(d-2))*(d-1)*d*
    332         SetProd(EvalSet(_.range.der.1[i], Real(Real h)
    333         {
    334           MatDat(x,1,h)^_.degComb[k][h]
    335         }))
    336       })
    337     },
    338     {
    339       Real d.i = _.degComb[k][i];
    340       Real d.j = _.degComb[k][j];
    341       If(Or(d.i==0, d.j==0), 0,
    342       {
    343         (MatDat(x,1,i)^(d.i-1))*d.i*
    344         (MatDat(x,1,j)^(d.j-1))*d.j*
    345         SetProd(EvalSet(_.range.der.2[i][j], Real(Real h)
    346         {
    347           MatDat(x,1,h)^_.degComb[k][h]
    348         }))
    349       })
    350     })
    351   };
    352 
    353   //For efficience reasonsthe base has specific method for evaluation over
    354   //massive data stored in a matrix
    355 
    356   ////////////////////////////////////////////////////////////////////////////
    357   Static Text _.autodoc.member.function.v =
    358   "Evaluates the k-th basic function for an "
    359   "array of points given as the rows of a matrix ";
    360   VMatrix function.v(Real k, VMatrix X)
    361   ////////////////////////////////////////////////////////////////////////////
    362   {
    363     BinGroup("$*", For(1,_.n, VMatrix(Real i)
    364     {
    365       RPow(SubCol(X,[[i]]),_.degComb[k][i])
    366     }))
    367   };
    368   ////////////////////////////////////////////////////////////////////////////
    369   Static Text _.autodoc.member.partial.derivative.1.v =
    370   "Evaluates the first partial derivative of k-th basic function for an "
    371   "array of points given as the rows of a matrix";
    372   VMatrix partial.derivative.1.v(Real k, VMatrix X, Real i)
    373   ////////////////////////////////////////////////////////////////////////////
    374   {
    375     Real d = _.degComb[k][i];
    376     If(d==0, Rand(VRows(X),1,0,0),
    377     {
    378       VMatrix aux = (RPow(SubCol(X,[[i]]),d-1)*d);
    379       If(!Card(_.range.der.1[i]),aux,aux $*
    380       BinGroup("$*", EvalSet(_.range.der.1[i], VMatrix rd1(Real h)
    381       {
    382         RPow(SubCol(X,[[h]]),_.degComb[k][h])
    383       })))
    384     })
    385   };
    386   ////////////////////////////////////////////////////////////////////////////
    387   Static Text _.autodoc.member.partial.derivative.2.v =
    388   "Evaluates the second partial derivative of k-th basic function for an "
    389   "array of points given as the rows of a matrix";
    390   VMatrix partial.derivative.2.v(Real k, VMatrix X, Real i, Real j)
    391   ////////////////////////////////////////////////////////////////////////////
    392   {
    393     If(i==j,
    394     {
    395       Real d = _.degComb[k][i];
    396       If(d<=1, Rand(VRows(X),1,0,0),
    397       {
    398         VMatrix aux = (RPow(SubCol(X,[[i]]),d-2)*((d-1)*d));
    399         If(!Card(_.range.der.1[i]),aux,aux $*
    400         BinGroup("$*", EvalSet(_.range.der.1[i], VMatrix rd21(Real h)
    401         {
    402           RPow(SubCol(X,[[h]]),_.degComb[k][h])
    403         })))
    404       })
    405     },
    406     {
    407       Real d.i = _.degComb[k][i];
    408       Real d.j = _.degComb[k][j];
    409       If(Or(d.i==0, d.j==0), Rand(VRows(X),1,0,0),
    410       {
    411         VMatrix aux = (RPow(SubCol(X,[[i]]),d.i-1)*d.i) $*
    412                       (RPow(SubCol(X,[[j]]),d.j-1)*d.j);
    413         If(!Card(_.range.der.2[i][j]),aux,aux $*
    414         BinGroup("$*", EvalSet(_.range.der.2[i][j], VMatrix rd22(Real h)
    415         {
    416           RPow(SubCol(X,[[h]]),_.degComb[k][h])
    417         })))
    418       })
    419     })
    420   }
    421 }
     17   "vdebuen@tol-project.org"   ]]
    42218
    42319
Note: See TracChangeset for help on using the changeset viewer.