1 #ifndef CORE_StdValArray_H
2 #define CORE_StdValArray_H
4 #include "CORE_ValArray.h"
7 #include "functions_numeric.h"
70 static inline CORE_UniquePointer<Self>
New() {
103 copy(values.size(),values);
110 copy(values.size(),values);
119 template<
size_t N,
typename Q>
132 copy(values.size(),values);
142 copy(values.size(),values);
165 template<
typename Q,
class I1>
175 template<
typename Q,
class I1>
193 template<
typename Q,
class I>
203 template<
typename Q,
class I>
213 template<
typename Q,
class I>
223 template<
typename Q,
class I>
225 Copy(cpy.getArray(),this->getArray());
234 Copy(cpy.getArray(),this->getArray());
241 Copy(cpy.getArray(),this->getArray());
252 inline void copy(
const tIndex& n ,
const Q* Vs) {
260 inline void copy(
const tIndex& n,
const std::initializer_list<T>& Vs) {
261 tIndex p=functions_numeric::min(n,Vs.size());
268 inline void copy(
const tIndex& n,std::initializer_list<T>&& Vs) {
269 tIndex p=functions_numeric::min(n,Vs.size());
278 template<
typename Q,
size_t N>
279 inline void copy(
const tIndex& n,
const std::array<Q,N>& Vs) {
280 tIndex p=functions_numeric::min(n,N);
281 Copy(p,Vs.getData(),this->getArray());
290 inline void copy(
const tIndex& n,
const std::valarray<Q>& Vs) {
291 tIndex p=functions_numeric::min(n,Vs.size());
300 inline void copy(
const tIndex& n,
const std::vector<Q>& Vs) {
301 tIndex p=functions_numeric::min(n,Vs.size());
316 inline static void Copy(
const std::valarray<T>& X,std::valarray<T>& R) {
325 template<
typename Q,
class I>
329 std::for_each(std::ranges::begin(R),std::ranges::end(R),[&iX](
auto& Ri){Ri=(*iX);iX++;});
340 inline static void Copy(
const tIndex& n,
const std::valarray<Q>& X,std::valarray<T>& R) {
347 std::for_each(std::ranges::begin(R),std::ranges::end(R),[&iX](
auto& Ri){Ri=(*iX);iX++;});
358 inline static void Copy(
const tIndex& n,std::initializer_list<T>&& X,std::valarray<T>& R) {
365 std::for_each(std::ranges::begin(R),std::ranges::end(R),[&iX](
auto& Ri){Ri=(*iX);iX++;});
377 inline static void Copy(
const tIndex& n,std::initializer_list<T> X,std::valarray<T>& R) {
384 std::for_each(std::ranges::begin(R),std::ranges::end(R),[&iX](
auto& Ri){Ri=(*iX);iX++;});
398 inline static void Copy(
const tIndex& n,
const std::vector<Q>& X,std::valarray<T>& R) {
401 std::for_each(std::ranges::begin(R),std::ranges::end(R),[&iX](
auto& Ri){Ri=(*iX);iX++;});
413 inline static void Copy(
const tIndex& n,
const Q* X,std::valarray<T>& R) {
416 std::for_each(std::ranges::begin(R),std::ranges::end(R),[&iX](
auto& Ri){Ri=(*iX);iX++;});
430 for(
auto& Ri: this->
getArray()) {Ri=x;};
441 T alpha=(max-min)/RAND_MAX;
442 for(
auto& V : values) {V=alpha*((T)std::rand())+min;};
450 T alpha=(max-min)/RAND_MAX;
451 for(
auto& V : values) {V=alpha*((T)std::rand())+min;};
461 T alpha=(max-min)/RAND_MAX;
462 for(
auto& V : values) {V=alpha*((T)std::rand())+min;};
509 if (fabs(v)<std::numeric_limits<T>::epsilon()) {
511 "CORE_StdValArray/=",
531 inline Self& operator&=(
const T& v) requires functions_type::isIntegerType<T> {
539 inline Self& operator|=(
const T& v) requires functions_type::isIntegerType<T> {
547 inline Self& operator^=(
const T& v) requires functions_type::isIntegerType<T> {
554 inline Self& operator<<=(
const T& v) requires functions_type::isIntegerType<T> {
562 inline Self& operator>>=(
const T& v) requires functions_type::isIntegerType<T> {
576 template<
typename Q,
class I>
579 transform([&](
const auto& x,
const auto&y){
return x+y;},(*this),X);
589 template<
typename Q,
class I>
592 transform([&](
const auto& x,
const auto&y){
return x-y;},(*this),X);
601 template<
typename Q,
class I>
604 transform([&](
const auto& x,
const auto&y){
return x*y;},(*this),X);
615 template<
typename Q,
class I>
618 transform([&](
const auto& x,
const auto&y){
619 if (y==0)
throw CORE_Exception(
"core",
"CORE_Array<T>::/=",
"division by zero");
673 template<
typename LambdaFct>
683 template<
typename LambdaFct>
695 template<
typename LambdaFct>
710 template<
typename LambdaFct>
713 std::transform(R.cbegin(),R.cend(),R.begin(),F);
723 template<
typename LambdaFct>
727 R.setSize(X.getsize());
728 std::transform(X.
cbegin(),X.
cend(),R.begin(),F);
738 template<
typename LambdaFct>
745 "CORE_StdValArray::Transform(F,X,Y,R)",
746 "X ("+std::to_string(X.
getSize())+
") & Y ("+std::to_string(Y.
getSize())+
") have not same size");
763 for(
const auto& Vi : this->
getArray()) {
767 if (norm>std::numeric_limits<T>::epsilon()) {
782 template<
typename Q,
class I>
784 AXPY(alpha,X,beta,*
this);
797 template<
typename Q,
class I>
802 [&alpha,&beta](
const auto& Xi,
const auto& Yi) {
803 return beta*Yi+alpha*Xi;
819 return (std::find_if(this->
cbegin(),
822 return std::isnan(v);
843 std::for_each(Aout.
begin(),Aout.
end(),
855 inline void sum(T& s)
const {
863 for(
const auto& Vi : this->
getArray()) p*=Vi;
874 template<
typename Q,
class I>
879 std::for_each_n(X.
cbegin(),n,[&iV,&s](
const auto& Xi) {s+=Xi*(*iV);iV++;});
889 for(
const auto& Vi : this->
getArray()) s+=Vi*Vi;
899 template<
typename Q,
class I>
907 tIndex p=functions_numeric::min(X.
getSize(),this->getSize());
913 std::for_each_n(this->
cbegin(),p,
914 [&iX,&s,&tmp](
const auto& Vi) {
929 [&s](
const auto& Vi) {
942 if (this->
getSize()==0)
return s;
949 for(
const auto& Vi : this->
getArray()) {
966 template<
typename Q,
class I>
974 tIndex p=functions_numeric::min(X.
getSize(),this->getSize());
982 std::for_each_n(this->
cbegin(),p,
983 [&iX,&i,&imax,&s,&tmp](
const auto& Vi) {
984 tmp=std::fabs(Vi-(*iX));
995 tmp=std::fabs((*iX));
1006 [&imax,&s,&tmp,&i](
const auto& Vi) {
1022 inline void min(T& m)
const requires functions_type::isOrderedType<T> {
1028 inline void max(T& m)
const requires functions_type::isOrderedType<T> {
1035 inline void directionalSort(
const tUChar& order) requires functions_type::isOrderedType<T>{
1038 std::sort(this->
begin(),this->
end(),std::less<T>());
1041 std::sort(this->
begin(),this->
end(),std::greater<T>());
this class describes an array of values T of dynamical size with algebrical operators and I is an imp...
Definition: CORE_Array.h:91
const T * getValues() const
get the values of the array for reading
Definition: CORE_Array.h:206
constexpr auto cbegin() const
return begin iterator for reading
Definition: CORE_Collection.h:143
auto begin()
return begin iterator for writing
Definition: CORE_Collection.h:155
auto end()
return end iterator for writing
Definition: CORE_Collection.h:161
tIndex getSize() const
return the size of the container
Definition: CORE_Collection.h:111
constexpr auto cend() const
return end iterator for reading
Definition: CORE_Collection.h:149
this class describes the exceptions raised for CORE package
Definition: CORE_Exception.h:17
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:113
this class describes a standart arithmetic array type implemented with a std::valarray object of type...
Definition: CORE_StdValArray.h:14
Self & operator=(const std::vector< Q > &values)
build an array by a copy of c
Definition: CORE_StdValArray.h:141
T & scalarProduct(const CORE_Array< Q, I > &X, T &s) const
return the scalar product
Definition: CORE_StdValArray.h:875
static CORE_UniquePointer< Self > New()
return a new unique pointer of this
Definition: CORE_StdValArray.h:70
void AXPY(const Q &alpha, const CORE_Array< Q, I > &X, const T &beta, Self &Y)
compute Y=beta.Y+ alpha .X
Definition: CORE_StdValArray.h:798
static Self & Sum(const Self &Ain, Self &Aout)
sum the element of the 2 arrays
Definition: CORE_StdValArray.h:838
static void UniformRandomize(const T &min, const T &max, std::vector< T > &values)
generate a random val array
Definition: CORE_StdValArray.h:440
void initialize(const T &x)
initialize the array to the value x
Definition: CORE_StdValArray.h:429
tReal l2Norm2() const
computes the L2-Norm of this
Definition: CORE_StdValArray.h:887
void copy(const tIndex &n, const std::vector< Q > &Vs)
initialize the array to the values of vector
Definition: CORE_StdValArray.h:300
void normalize()
normalize the array
Definition: CORE_StdValArray.h:760
static void Transform(LambdaFct &&F, const CORE_Array< T, Self > &X, Self &R)
apply the transform element with the lambda function Ri = F(const Xi)
Definition: CORE_StdValArray.h:724
Self & operator=(const T &v)
fill the values of the array with v
Definition: CORE_StdValArray.h:95
CORE_StdValArray(const tIndex &n)
build an instance of class of size n
Definition: CORE_StdValArray.h:33
static void Transform(LambdaFct &&F, const CORE_Array< T, Self > &X, const CORE_Array< T, Self > &Y, Self &R)
apply the transform element with the lambda function Ri = F(const Xi,const Yi)
Definition: CORE_StdValArray.h:739
CORE_StdValArray()
build an instance of class
Definition: CORE_StdValArray.h:27
void axpy(const Q &alpha, const CORE_Array< Q, I > &X, const T &beta)
compute This=beta.This+ alpha .X
Definition: CORE_StdValArray.h:783
void copy(const Self &cpy)
copy the container
Definition: CORE_StdValArray.h:233
void prod(T &p) const
return the product of all the elements
Definition: CORE_StdValArray.h:861
void copy(CORE_ValArray< Q, I > &&cpy)
copy the conatiner : mv is destroyed after this
Definition: CORE_StdValArray.h:224
static void UniformRandomize(const T &min, const T &max, std::valarray< T > &values)
generate a random val array
Definition: CORE_StdValArray.h:449
static void Copy(const tIndex &n, std::initializer_list< T > &&X, std::valarray< T > &R)
copy the array R=X
Definition: CORE_StdValArray.h:358
static void Copy(const tIndex &n, std::initializer_list< T > X, std::valarray< T > &R)
copy the array R=X
Definition: CORE_StdValArray.h:377
void copy(Self &&cpy)
copy the conatiner : mv is destroyed after this
Definition: CORE_StdValArray.h:240
Self & operator=(const std::array< Q, N > &values)
build an array by a copy of c
Definition: CORE_StdValArray.h:120
Self & operator-=(const Self &X)
array add operator : This -=X
Definition: CORE_StdValArray.h:639
static void UniformRandomize(const T &min, const T &max, std::array< T, N > &values)
generate a random val array
Definition: CORE_StdValArray.h:460
void copy(const CORE_Array< Q, I > &X)
copy the array
Definition: CORE_StdValArray.h:194
void copy(const CORE_ValArray< Q, I > &cpy)
copy the container
Definition: CORE_StdValArray.h:214
static void Copy(const std::valarray< T > &X, std::valarray< T > &R)
copy 2 valarray with same type
Definition: CORE_StdValArray.h:316
static void Copy(const tIndex &n, const std::vector< Q > &X, std::valarray< T > &R)
copy the array R=X
Definition: CORE_StdValArray.h:398
void transform(LambdaFct &&F, const CORE_Array< T, Self > &X)
transform the transform element with the lambda function Ti = F(const Xi)
Definition: CORE_StdValArray.h:684
static void Copy(const tIndex &n, const Q *X, std::valarray< T > &R)
copy the array
Definition: CORE_StdValArray.h:413
tReal linfDistance(tIndex &imax) const
compute the L infinity norm of This
Definition: CORE_StdValArray.h:939
Self & operator=(const Self &&values)
build an array by a copy of c (in mirror with the copy operator)
Definition: CORE_StdValArray.h:155
virtual tMemSize getMemorySize() const override
return the memory size of the class
Definition: CORE_StdValArray.h:59
Self & operator*=(const CORE_Array< Q, I > &X)
array multiply operator This *=X
Definition: CORE_StdValArray.h:602
Self & operator/=(const CORE_Array< Q, I > &X)
array divisor operator This /=X
Definition: CORE_StdValArray.h:616
Self & operator=(const CORE_Array< Q, I1 > &values)
build an array by a copy of c
Definition: CORE_StdValArray.h:166
static void Copy(const CORE_Array< Q, I > &X, std::valarray< T > &R)
copy 2 array
Definition: CORE_StdValArray.h:326
virtual ~CORE_StdValArray()
destroy an instance of class
Definition: CORE_StdValArray.h:40
void transform(LambdaFct &&F, const CORE_Array< T, Self > &X, const CORE_Array< T, Self > &Y)
transform the transform element with the lambda function Ti = F(const Xi,const Yi)
Definition: CORE_StdValArray.h:696
Self & operator-=(const T &v)
sub operator
Definition: CORE_StdValArray.h:491
Self & operator=(const Self &values)
build an array by a copy of c
Definition: CORE_StdValArray.h:148
static void Copy(const tIndex &n, const std::valarray< Q > &X, std::valarray< T > &R)
copy the first n values of a std::valarray
Definition: CORE_StdValArray.h:340
void copy(const tIndex &n, const Q *Vs)
initialize the array to the values of pointer of size n
Definition: CORE_StdValArray.h:252
static void Transform(LambdaFct &&F, Self &R)
apply the transform element with the lambda function Ri = F(const Ri)
Definition: CORE_StdValArray.h:711
void sum(T &s) const
return the sum of all the elements
Definition: CORE_StdValArray.h:855
Self & operator/=(const T &v)
divisor operator
Definition: CORE_StdValArray.h:508
Self & operator=(const std::valarray< Q > &values)
build an array by a copy of c
Definition: CORE_StdValArray.h:131
Self & operator*=(const Self &X)
array add operator : This *=X
Definition: CORE_StdValArray.h:649
Self & operator*=(const T &v)
multiplicator operator
Definition: CORE_StdValArray.h:500
void transform(LambdaFct &&F)
transform the transform element with the lambda function Ti = F(const Ti)
Definition: CORE_StdValArray.h:674
Self & operator+=(const Self &X)
array add operator : This +=X
Definition: CORE_StdValArray.h:629
Self & operator=(std::initializer_list< T > &&values)
build an array by a copy of c
Definition: CORE_StdValArray.h:109
void copy(const tIndex &n, const std::initializer_list< T > &Vs)
initialize the array to the values of list
Definition: CORE_StdValArray.h:260
void copy(const tIndex &n, const std::array< Q, N > &Vs)
initialize the array to the values of array of size N
Definition: CORE_StdValArray.h:279
void copy(const tIndex &n, const std::valarray< Q > &Vs)
initialize the array to the values of val array
Definition: CORE_StdValArray.h:290
Self & operator-=(const CORE_Array< Q, I > &X)
array sub operator This -=X
Definition: CORE_StdValArray.h:590
tReal l2Distance2(const CORE_Array< Q, I > &X) const
compute the square of distance of this to X
Definition: CORE_StdValArray.h:900
Self & operator=(const std::initializer_list< T > &values)
build an array by a copy of c
Definition: CORE_StdValArray.h:102
void copy(const tIndex &n, std::initializer_list< T > &&Vs)
initialize the array to the values of list
Definition: CORE_StdValArray.h:268
void uniformRandomize(const T &min, const T &max)
randomize the vector in [min,max]
Definition: CORE_StdValArray.h:471
Self & operator+=(const T &v)
add operator
Definition: CORE_StdValArray.h:484
tReal linfDistance(const CORE_Array< Q, I > &X, tIndex &imax) const
compute the L infinity distance of this to X
Definition: CORE_StdValArray.h:967
Self & operator%=(const T &v) requires functions_type
modulo operator
Definition: CORE_StdValArray.h:523
Self & operator=(const CORE_Array< Q, I1 > &&values)
build an array by a copy of c (in mirror with the copy operator)
Definition: CORE_StdValArray.h:176
tBoolean isNANContained() const
return true if one value is Not A Number
Definition: CORE_StdValArray.h:816
Self & operator/=(const Self &X)
array add operator : This /=X
Definition: CORE_StdValArray.h:658
void copy(CORE_Array< Q, I > &&X)
copy the array
Definition: CORE_StdValArray.h:204
this class describes an arithmetic array type implemented with a std::valarray object with implementa...
Definition: CORE_ValArray.h:70
constexpr auto cend() const
return end iterator for reading
Definition: CORE_ValArray.h:205
constexpr auto cbegin() const
return begin iterator for reading
Definition: CORE_ValArray.h:199
virtual tMemSize getContentsMemorySize() const override
return the memory size of the included associations
Definition: CORE_ValArray.h:126
tIndex getSize() const
return the size of the array for writing
Definition: CORE_ValArray.h:147
auto end()
return end iterator for writing
Definition: CORE_ValArray.h:217
auto begin()
return begin iterator for writing
Definition: CORE_ValArray.h:211
std::valarray< T > & getArray()
get the array
Definition: CORE_ValArray.h:257