-
Notifications
You must be signed in to change notification settings - Fork 299
/
Polynomial_LD.inl
702 lines (658 loc) · 20.7 KB
/
Polynomial_LD.inl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#ifndef SOFA_HELPER_POLYNOMIAL_LD_INL
#define SOFA_HELPER_POLYNOMIAL_LD_INL
#include <sofa/helper/Polynomial_LD.h>
#include <sofa/helper/logging/Messaging.h>
#include <sstream>
#include <iterator>
namespace sofa::helper
{
/**
* Tools used in FEM computing
*/
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
template<typename Real, unsigned int N>
Monomial_LD<Real,N>::Monomial_LD()
{
coef=(Real) 0;
for(unsigned int i=0; i<N; i++)
{
powers[i]=0;
std::ostringstream oss; oss << 't' << i ;
variables[i]=oss.str();
}
}
////////////////////////////////
template<typename Real, unsigned int N>
Monomial_LD<Real,N>::Monomial_LD(const Monomial_LD<Real,N> & b)
{
coef=b.coef;
for(unsigned int i=0; i<N; i++)
{
powers[i]=b.powers[i];
variables[i]=b.variables[i];
}
}
////////////////////////////////
template<typename Real, unsigned int N>
Monomial_LD<Real,N>& Monomial_LD<Real,N>::operator=(const Monomial_LD<Real,N> & b)
{
coef=b.coef;
for( unsigned int i=0; i<N; i++)
{
powers[i]=b.powers[i];
variables[i]=b.variables[i];
}
return *this;
}
////////////////////////////////
template<typename Real, unsigned int N>
int Monomial_LD<Real,N>::degree()
{
int degree=0;
for(unsigned int i=0; i<N; i++)
degree+=powers[i];
return degree;
}
////////////////////////////////
template<typename Real, unsigned int N>
bool Monomial_LD<Real,N>::operator==(const Monomial_LD<Real,N> & b) const
{
bool compare=true;
if ((coef != b.coef) || (variables.size() != b.variables.size())) compare=false;
for(unsigned int i=0; i<N; i++)
{
if (powers[i] != b.powers[i]) compare=false;
}
return compare;
}
////////////////////////////////
template<typename Real, unsigned int N>
bool Monomial_LD<Real,N>::isSamePowers(const Monomial_LD<Real,N> & b) const
{
bool compare=true;
if ( variables.size() != b.variables.size() ) compare=false;
for(unsigned int i=0; i<N; i++)
{
if (powers[i] != b.powers[i]) compare=false;
}
return compare;
}
////////////////////////////////
template<typename Real, unsigned int N>
Monomial_LD<Real,N> & Monomial_LD<Real,N>::operator+=(const Monomial_LD<Real,N> & b)
{
if (this->isSamePowers(b))
{
this->coef+=b.coef;
}
else
{
dmsg_warning("Monomial_LD") <<(*this)<<" + "<<b <<" Not permissed with different powers" ;
}
return *this;
}
////////////////////////////////
template<typename Real, unsigned int N>
Monomial_LD<Real,N> & Monomial_LD<Real,N>::operator-=(const Monomial_LD<Real,N> & b)
{
if (this->isSamePowers(b))
{
this->coef-=b.coef;
}
else
{
dmsg_warning("Monomial_LD") <<(*this)<<" - "<<b <<" Not permissed with different powers" ;
}
return *this;
}
////////////////////////////////
template<typename Real, unsigned int N>
Monomial_LD<Real,N> & Monomial_LD<Real,N>::operator*=(const Monomial_LD<Real,N> & b)
{
///////////////////////////////////////////////////////
coef*=b.coef;
for(unsigned int i=0; i<N; i++)
powers[i] += b.powers[i];
return *this;
}
////////////////////////////////
template<typename Real, unsigned int N>
Real Monomial_LD<Real,N>::operator()(const RNpoint & x) const
{
dmsg_warning_when(x.size()!= N, "Monomial_LD") << "Value assigned to the monome has not the good number of variable." ;
Real value= coef;
for( unsigned int i=0; i<N; i++)
{
value*=(Real) pow(x[i],powers[i]);
}
return value;
}
////////////////////////////////
template<typename Real, unsigned int N>
Real Monomial_LD<Real,N>::operator()(const RNpoint & x,unsigned int idvar) const
{
dmsg_warning_when(x.size()!= N, "Monomial_LD") <<"Value assigned to the monome has not the good number of variable" ;
Real value= coef;
if (idvar >= N)
{
dmsg_warning("Monomial_LD") <<idvar<<"-th partial derivative couldn't take place for the monomial of:"<<N<<"-variables"<<msgendl
<<(*this)<< msgendl
<<"CONDITION: id_variable = { 0,1... (NbVariable-1) }";
}
else
{
for(unsigned int i=0; i<N; i++)
{
if (i!=idvar)
{
value*=(Real) pow(x[i],powers[i]);
}
else if(powers[i]>0)
{
value*=(Real) powers[i];//derivate
value*=(Real) pow(x[i],(powers[i]-1));
}
else
{
value *= (Real)0.;
}
}
}
return value;
}
////////////////////////////////
template<typename Real, unsigned int N>
Monomial_LD<Real,N> Monomial_LD<Real,N>::d(const unsigned int & idvar) const
{
Monomial_LD<Real,N> r(*this);
if (idvar >= N)
{
msg_warning("Monomial_LD") << idvar<<"-th derivative couldn't take place for the monomial of:"<<N<<"-variables"<<msgendl
<<r<<msgendl
<<"CONDITION: id_derivative = { 0,1... (NbVariable-1) }" ;
}
else
{
r.coef*=(Real) r.powers[idvar];
if (powers[idvar] != 0)
{
(r.powers[idvar])--;
}
}
return r;
}
////////////////////////////////
template<typename Real, unsigned int N>
void Monomial_LD<Real,N>::writeToStream(std::ostream & stream) const
{
stream<<coef<<"*"<<variables[0]<<"^"<<powers[0];
for(unsigned int i=1; i<N; i++) stream<<"*"<<variables[i]<<"^"<<powers[i];
}
////////////////////////////////
template<typename Real, unsigned int N>
void Monomial_LD<Real,N>::readFromStream(std::istream & stream)
{
Real t_coef; int t_power;
if (stream >> t_coef ) coef=t_coef;
for(unsigned int i=0; i<N; ++i)
{
if (stream >> t_power) powers[i]=t_power;
}
}
template< typename FReal, unsigned int FN > //For comutativity of operator *: Monomial_LD*Real || Real*Monomial_LD.
Monomial_LD< FReal, FN > & operator*(const FReal & alpha,Monomial_LD< FReal, FN > & r)
{
r *= alpha;
return r;
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
////////////////////////////////
template<typename Real, unsigned int N>
Polynomial_LD<Real,N>::Polynomial_LD()
{
nbOfMonomial=0;
}
////////////////////////////////
template<typename Real, unsigned int N>
Polynomial_LD<Real,N>::Polynomial_LD(const Polynomial_LD<Real,N> & a)
{
listOfMonoMial=a.listOfMonoMial;
nbOfMonomial=a.nbOfMonomial;
}
////////////////////////////////
template<typename Real, unsigned int N>
Polynomial_LD<Real,N>::Polynomial_LD(const Monomial_LD<Real,N> & a)
{
listOfMonoMial.push_back(a);
nbOfMonomial=1;
}
////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////
template<typename Real, unsigned int N>
Monomial_LD<Real,N>::Monomial_LD(Real m_coef, ...)
{
coef=m_coef;
va_list vl;
va_start(vl,m_coef);
for(unsigned int i=0; i<N; i++)
{
powers[i]=va_arg(vl,int);
std::ostringstream oss; oss << 'l' << i ;
variables[i]=oss.str();
}
va_end(vl);
}
template<typename Real, unsigned int N>
Polynomial_LD<Real,N>::Polynomial_LD(const unsigned int nbofTerm,...)
{
nbOfMonomial=nbofTerm;
va_list vl;
va_start(vl,nbofTerm);
for (unsigned int iterm=0; iterm<nbofTerm; iterm++)
{
Monomial_LD<Real,N> mi;
double coefindouble=va_arg(vl, double );
Real coefi=(Real)coefindouble;
mi.SetCoef(coefi);
for(unsigned int jvar=0; jvar<N; jvar++)
{
int m_power=va_arg(vl,int); mi.SetPower(jvar,m_power);
}
listOfMonoMial.push_back(mi);
}
va_end(vl);
this->sort();
}
////////////////////////////////
template<typename Real, unsigned int N>
void Polynomial_LD<Real,N>::Set(const unsigned int nbofTerm,...)
{
nbOfMonomial= nbofTerm;
if(nbOfMonomial!=nbofTerm)
{
nbOfMonomial=nbofTerm ; listOfMonoMial.resize(nbOfMonomial);
}
va_list vl;
va_start(vl,nbofTerm);
for (unsigned int iterm=0; iterm<nbofTerm; iterm++)
{
Monomial_LD<Real,N> mi;
double coefindouble=va_arg(vl, double );
Real coefi=(Real)coefindouble;
mi.SetCoef(coefi); mi.SetCoef(coefi);
for(unsigned int jvar=0; jvar<N; jvar++)
{
int m_power=va_arg(vl,int); mi.SetPower(jvar,m_power);
}
listOfMonoMial[iterm]=mi;
}
va_end(vl);
this->sort();
}
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////
template<typename Real, unsigned int N>
int Polynomial_LD<Real,N>::degree()
{
int deg=0;
for(MonomialIterator it=listOfMonoMial.begin(); it != listOfMonoMial.end(); ++it)
{
deg = ( (*it).degree() > deg ) ? (*it).degree() : deg;
}
return deg;
}
////////////////////////////////
template<typename Real, unsigned int N>
bool Polynomial_LD<Real,N>::operator ==(const Polynomial_LD<Real,N> & b) const
{
bool result=true;
if ( this->listOfMonoMial.size() != b.listOfMonoMial.size() )
result=false;
else
{
result = ( this->listOfMonoMial == b.listOfMonoMial ) ? result : false;
}
return result;
}
////////////////////////////////
template<typename Real, unsigned int N>
Polynomial_LD<Real,N> & Polynomial_LD<Real,N>::operator*=(const Real & alpha)
{
for(MonomialIterator it=listOfMonoMial.begin(); it != listOfMonoMial.end(); ++it)
{
(*it)*=alpha;
}
return *this;
}
////////////////////////////////
template<typename Real, unsigned int N>
Polynomial_LD<Real,N> & Polynomial_LD<Real,N>::operator/=(const Real & alpha)
{
for(MonomialIterator it=listOfMonoMial.begin(); it != listOfMonoMial.end(); ++it)
{
//*it->operator/=(alpha);
(*it)/=alpha;
}
return *this;
}
////////////////////////////////
template<typename Real, unsigned int N>
Polynomial_LD<Real,N> & Polynomial_LD<Real,N>::operator+=(const Monomial_LD<Real,N> & b)
{
bool added=false;
for(MonomialIterator ita=listOfMonoMial.begin(); ita != listOfMonoMial.end(); ++ita)
{
if ( (*ita).isSamePowers(b) )
{
(*ita)+=b;
added=true;
break;
}
}
if (!added)
{
listOfMonoMial.push_back(b);
nbOfMonomial++;
}
sort();
return *this;
}
////////////////////////////////
////////////////////////////////
template<typename Real, unsigned int N>
Polynomial_LD<Real,N> & Polynomial_LD<Real,N>::operator+=(const Polynomial_LD<Real,N> & b)
{
for(MonomialConstIterator itb=b.listOfMonoMial.begin(); itb != b.listOfMonoMial.end(); ++itb)
{
bool added=false;
for(MonomialIterator ita=listOfMonoMial.begin(); ita != listOfMonoMial.end(); ++ita)
{
if ( (*ita).isSamePowers(*itb) )
{
(*ita)+=(*itb);
added=true;
break;
}
}
if (!added)
{
listOfMonoMial.push_back((*itb));
nbOfMonomial++;
}
}
sort();
return *this;
}
////////////////////////////////
template<typename Real, unsigned int N>
Polynomial_LD<Real,N> & Polynomial_LD<Real,N>::operator-=(const Polynomial_LD<Real,N> & b)
{
for(MonomialConstIterator itb=b.listOfMonoMial.begin(); itb != b.listOfMonoMial.end(); ++itb)
{
bool added=false;
for(MonomialIterator ita=listOfMonoMial.begin(); ita != listOfMonoMial.end(); ++ita)
{
if ( (*ita).isSamePowers(*itb) )
{
(*ita)-=(*itb);
added=true;
break;
}
}
if (!added)
{
listOfMonoMial.push_back(-(*itb));
nbOfMonomial++;
}
}
sort();
return *this;
}
////////////////////////////////
template<typename Real, unsigned int N>
Polynomial_LD<Real,N> & Polynomial_LD<Real,N>::operator*=(const Polynomial_LD<Real,N> & b)
{
Polynomial_LD<Real,N> old=(*this);
listOfMonoMial.resize(listOfMonoMial.size()*(b.listOfMonoMial.size()));
unsigned int m_counter=0;
for(unsigned int ita=0; ita<old.listOfMonoMial.size(); ita++)
{
for(unsigned int itb=0; itb<b.listOfMonoMial.size(); itb++)
{
listOfMonoMial[m_counter]=old.listOfMonoMial[ita]*b.listOfMonoMial[itb];
m_counter++;
}
}
this->sort();
return *this;
}
////////////////////////////////
template<typename Real, unsigned int N>
Polynomial_LD<Real,N> Polynomial_LD<Real,N>::operator-() const
{
Polynomial_LD<Real,N> r(*this);
for(MonomialIterator it=r.listOfMonoMial.begin(); it != r.listOfMonoMial.end(); ++it)
{
(*it).coef*=(Real) -1.;
}
r.sort();
return r;
}
////////////////////////////////
template<typename Real, unsigned int N>
Real Polynomial_LD<Real,N>::operator()(const RNpoint & x) const
{
Real result=(Real) 0.;
for(MonomialConstIterator it=listOfMonoMial.begin(); it != listOfMonoMial.end(); ++it)
{
result += (*it).operator()(x);
}
return result;
}
////////////////////////////////
template<typename Real, unsigned int N>
Real Polynomial_LD<Real,N>::operator()(const RNpoint & x,unsigned int iderive) const
{
Real result=(Real) 0.;
if (iderive >= N)
{
dmsg_warning("Polynomial_LD") <<iderive<<"-th derivative couldn't take place for the polynomial of:"<<N<<"-variables"<<msgendl
<<(*this)<<msgendl
<<"CONDITION: id_derivative = { 0,1... (NbVariable-1) }" ;
}
else
{
for(MonomialConstIterator it=listOfMonoMial.begin(); it != listOfMonoMial.end(); ++it)
{
result += it->operator()(x,iderive);
}
}
return result;
}
////////////////////////////////
template<typename Real, unsigned int N>
Polynomial_LD<Real,N> Polynomial_LD<Real,N>::d(const unsigned int & iderive) const
{
Polynomial_LD<Real,N> result(*this);
if (iderive >=N)
{
dmsg_warning("Polynomial_LD") <<iderive<<"-th derivative couldn't take place for the polynomial of:"<<"-variables" << msgendl
<<result<<msgendl
<<"CONDITION: id_derivative = { 0,1... (NbVariable-1) }" ;
}
else
{
for(MonomialIterator it=result.listOfMonoMial.begin(); it != result.listOfMonoMial.end(); ++it)
{
(*it).coef*=(Real) (*it).powers[iderive];
if ((*it).powers[iderive] != 0)
{
((*it).powers[iderive])--;
}
}
}
result.sort();
return result;
}
////////////////////////////////
////////////////////////////////
template<typename Real, unsigned int N>
void Polynomial_LD<Real,N>::setnbOfMonomial(int m_nbofmonomial)
{
listOfMonoMial.clear();
nbOfMonomial=m_nbofmonomial;
Monomial_LD<Real,N> monomialnullptr;
for(unsigned int i=0; i<nbOfMonomial; i++)
{
listOfMonoMial.push_back(monomialnullptr);
}
}
////////////////////////////////
template<typename Real, unsigned int N>
void Polynomial_LD<Real,N>::writeToStream(std::ostream & stream) const
{
MonomialConstIterator it=listOfMonoMial.begin();
stream<< *it; ++it;
while(it != listOfMonoMial.end() )
{
stream << " + "<<*it;
++it;
}
}
////////////////////////////////
template<typename Real, unsigned int N>
void Polynomial_LD<Real,N>::readFromStream(std::istream & stream)
{
int nbofmonomialtempo;
if (stream >> nbofmonomialtempo) nbOfMonomial=nbofmonomialtempo;
listOfMonoMial.resize(nbOfMonomial);
MonomialIterator it=listOfMonoMial.begin();
for(unsigned int monomialcounter=0; monomialcounter<nbOfMonomial; ++monomialcounter)
{
Monomial_LD<Real,N> tempo;
if (stream >> tempo) (*it)=tempo;
++it;
}
sort();
}
////////////////////////////////
template<typename Real, unsigned int N>
std::string Polynomial_LD<Real,N>::getString() const
{
std::ostringstream m_outstream;
m_outstream<<(*this);
return m_outstream.str();
}
////////////////////////////////
template< typename FReal, unsigned int FN >
Polynomial_LD< FReal, FN > & operator*(const FReal & alpha, Polynomial_LD< FReal, FN> & r)
{
r *= alpha;
return r;
}
////////////////////////////////
template< typename FReal, unsigned int FN >
Polynomial_LD< FReal, FN > & operator*(const Monomial_LD< FReal, FN > & a, Polynomial_LD< FReal, FN> & r)
{
r *= a;
r.sort();
return r;
}
////////////////////////////////
template<typename Real, unsigned int N>
void Polynomial_LD<Real,N>::exchangeMonomial(unsigned int ithMono,unsigned int jthMono)
{
Monomial_LD<Real,N> tempo;
tempo=listOfMonoMial[ithMono];
listOfMonoMial[ithMono]=listOfMonoMial[jthMono];
listOfMonoMial[jthMono]=tempo;
}
////////////////////////////////
template<typename Real, unsigned int N>
void Polynomial_LD<Real,N>::sortByVar(unsigned int idVar)
{
if(idVar==0) //First variable in Rd
{
for(unsigned int ith=0; ith < listOfMonoMial.size()-1; ith++)
for(unsigned int jth=ith+1; jth < listOfMonoMial.size(); jth++)
{
if (listOfMonoMial[ith].powers[idVar] < listOfMonoMial[jth].powers[idVar])
{
exchangeMonomial(ith,jth);
}
}
}
else
{
for(unsigned int ith=0; ith<listOfMonoMial.size()-1; ith++)
for(unsigned int jth=ith+1; jth<listOfMonoMial.size(); jth++)
{
if ( (listOfMonoMial[ith].powers[idVar] < listOfMonoMial[jth].powers[idVar])
&& (listOfMonoMial[ith].powers[idVar-1] == listOfMonoMial[ith].powers[idVar-1]) )
{
exchangeMonomial(ith,jth);
}
}
}
}
////////////////////////////////
template<typename Real, unsigned int N>
void Polynomial_LD<Real,N>::sort()
{
//fusion all monomials which differ only the coef
for(MonomialIterator ita=listOfMonoMial.begin(); ita != listOfMonoMial.end() ; ++ita)
{
MonomialIterator itb=ita; itb++;
if (itb==listOfMonoMial.end()) break;
while(itb!=listOfMonoMial.end())
{
if (ita->isSamePowers((*itb)))
{
ita->coef += (*itb).coef;
itb=listOfMonoMial.erase(itb); itb--;
}
++itb;
}
}
//Eliminate all term null monomial
for(MonomialIterator ita=listOfMonoMial.begin(); ita != listOfMonoMial.end() ; ++ita)
{
if ((*ita).coef == 0)
{
ita=listOfMonoMial.erase(ita); ita--;
}
}
//Sorting monomial by variable degree
for(unsigned int ithVar=0; ithVar<N; ithVar++)
{
sortByVar(ithVar);
}
}
} // namespace sofa::helper
#endif