diff --git a/build_linux64/libamscppnarray.linux64.a b/build_linux64/libamscppnarray.linux64.a index eb22e83..d6f2dd4 100644 Binary files a/build_linux64/libamscppnarray.linux64.a and b/build_linux64/libamscppnarray.linux64.a differ diff --git a/build_linux64/objstore/amscppnarray.o b/build_linux64/objstore/amscppnarray.o index c482dc4..51ebb3e 100644 Binary files a/build_linux64/objstore/amscppnarray.o and b/build_linux64/objstore/amscppnarray.o differ diff --git a/build_linux64/objstore/amscppnarray_inits.o b/build_linux64/objstore/amscppnarray_inits.o new file mode 100644 index 0000000..ea9bf28 Binary files /dev/null and b/build_linux64/objstore/amscppnarray_inits.o differ diff --git a/build_linux64/objstore/amscppnarray_random.o b/build_linux64/objstore/amscppnarray_random.o new file mode 100644 index 0000000..68ab044 Binary files /dev/null and b/build_linux64/objstore/amscppnarray_random.o differ diff --git a/build_linux64/tests b/build_linux64/tests index 1953c10..b136d0f 100644 Binary files a/build_linux64/tests and b/build_linux64/tests differ diff --git a/include/amscppnarray/amscppnarray.hpp b/include/amscppnarray/amscppnarray.hpp index 9bce8c9..8619c6f 100644 --- a/include/amscppnarray/amscppnarray.hpp +++ b/include/amscppnarray/amscppnarray.hpp @@ -15,8 +15,8 @@ namespace narray { // Forward declaration of the operator- template function -template class narray; -template narray operator-(const narray &n); +//template class narray; +//template narray operator-(const narray &n); typedef int64_t narray_size_t; @@ -48,6 +48,7 @@ template class narray ~narray(); int resize(const narray_size_t newlength); + const narray_size_t size() const; T& operator[](const narray_size_t ind); const T& operator[](const narray_size_t ind) const; @@ -71,14 +72,35 @@ template class narray narray operator*(const T val) const; narray operator/(const T val) const; template friend narray operator-(const narray &n); + narray applyfn(T (*fnptr)(T)) const; + + //min,max + T min() const; + T max() const; + //subarray operations + narray subarray(const narray_size_t ind1, const narray_size_t ind2) const; //permutations and sorting + + + }; +//Initializers +template narray arange(const T low, const T high, const T incr); +template narray zeros(const narray_size_t N); +template narray ones(const narray_size_t N); + +narray linspacef(const float low, const float high, narray_size_t N); +narray linspace(const double low, const double high, narray_size_t N); +///////// +//Tests// +///////// + void test_narray1(); void test_narray2(); void test_narray3(); @@ -88,5 +110,9 @@ void test_narray3(); #include #include +#include +#include + +#include #endif \ No newline at end of file diff --git a/include/amscppnarray/amscppnarray_impl.hpp b/include/amscppnarray/amscppnarray_impl.hpp index 30b6a56..00d93e7 100644 --- a/include/amscppnarray/amscppnarray_impl.hpp +++ b/include/amscppnarray/amscppnarray_impl.hpp @@ -297,6 +297,10 @@ template void narray::setall(const T& val) return; } +template const narray_size_t narray::size() const +{ + return length; +} }; //end namespace narray }; //end namespace ams diff --git a/include/amscppnarray/amscppnarray_implinits.hpp b/include/amscppnarray/amscppnarray_implinits.hpp new file mode 100644 index 0000000..1f20dd7 --- /dev/null +++ b/include/amscppnarray/amscppnarray_implinits.hpp @@ -0,0 +1,108 @@ +#ifndef __AMSCPPNARRAY_IMPLINITS_HPP__ +#define __AMSCPPNARRAY_IMPLINITS_HPP__ + +namespace ams +{ +namespace narray +{ + + template void narray_arange_tf( + narray *in, + const T low, const T high, const T incr, + int threadnum, int nthreads + ) + { + int I0,I1,Is,I; + Is = in->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1); + for(I=I0;Idata[I] = low + incr*I; + } + } + +template narray arange(const T low, const T high, const T incr) +{ + narray ret; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + narray_size_t N = (narray_size_t)((high-low)/incr); + if(N<=0) + { + N = 0; + } + ret.resize(N); + + if(ret.length==0) + { + return ret; + } + + if(ret.lengthnarray_max_threads) ? narray_max_threads : nthreads; + //if(nthreads<1) nthreads=1; + //if(nthreads>narray_max_threads) nthreads = narray_max_threads; + threads.resize(nthreads); + for(J=0;J, + &ret, + low,high,incr, + J,nthreads + ); + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + + + + return ret; +} + +template narray zeros(const narray_size_t N) +{ + narray ret; + ret.resize(N); + ret.setall(0); + + return ret; +} + +template narray ones(const narray_size_t N) +{ + narray ret; + ret.resize(N); + ret.setall(1); + + return ret; +} + + +}; +}; + +#endif \ No newline at end of file diff --git a/include/amscppnarray/amscppnarray_imploper.hpp b/include/amscppnarray/amscppnarray_imploper.hpp index 9efc485..0a1f9b3 100644 --- a/include/amscppnarray/amscppnarray_imploper.hpp +++ b/include/amscppnarray/amscppnarray_imploper.hpp @@ -223,7 +223,7 @@ namespace narray if(this->lengthlength;I++) ret.data[I] = this->data[I] + other.data[I]; + for(I=0;Ilength;I++) ret.data[I] = this->data[I] - other.data[I]; } else { @@ -730,6 +730,246 @@ namespace narray return ret; } + template void narray_applyfn_tf( + const narray *a, + T (*fnptr)(T), + narray *out, + int threadnum, int nthreads + ) + { + int I0,I1,Is,I; + + Is = a->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? a->length: Is*(threadnum+1); + for(I=I0;Idata[I] = fnptr(a->data[I]); + } + + return; + } + + template narray narray::applyfn(T (*fnptr)(T)) const + { + narray ret; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + ret.resize(this->length); + + if(this->lengthlength;I++) ret.data[I] = fnptr(this->data[I]); + } + else + { + //threaded operation + nthreads = std::thread::hardware_concurrency(); + nthreads = (nthreads<1) ? 1 : nthreads; + nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; + //if(nthreads<1) nthreads=1; + //if(nthreads>narray_max_threads) nthreads = narray_max_threads; + threads.resize(nthreads); + for(J=0;J, + this, + fnptr, + &ret, + J,nthreads + ); + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + return ret; + } + + template void narray_min_tf( + const narray *in, + narray *out, + int threadnum, int nthreads + ) + { + int I0,I1,Is,I; + + Is = in->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1); + for(I=I0;Idata[I]data[threadnum]) + out->data[threadnum] = in->data[I]; + } + + return; + } + + template T narray::min() const + { + T ret = T(); + narray minarr; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + if(this->length<=0) + { + return ret; + } + + if(this->lengthdata[0]; + for(I=0;Ilength;I++) + { + if(this->data[I]data[I]; + } + } + } + else + { + //threaded operation + + nthreads = std::thread::hardware_concurrency(); + nthreads = (nthreads<1) ? 1 : nthreads; + nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; + //if(nthreads<1) nthreads=1; + //if(nthreads>narray_max_threads) nthreads = narray_max_threads; + threads.resize(nthreads); + minarr.resize(nthreads); + minarr.setall(this->data[0]); + for(J=0;J, + this, + &minarr, + J,nthreads + ); + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + ret = this->data[0]; + for(J=0;J void narray_max_tf( + const narray *in, + narray *out, + int threadnum, int nthreads + ) + { + int I0,I1,Is,I; + + Is = in->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1); + for(I=I0;Idata[I]>out->data[threadnum]) + out->data[threadnum] = in->data[I]; + } + + return; + } + + template T narray::max() const + { + T ret = T(); + narray maxarr; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + + if(this->length<=0) + { + return ret; + } + + if(this->lengthdata[0]; + for(I=0;Ilength;I++) + { + if(this->data[I]>ret) + { + ret = this->data[I]; + } + } + } + else + { + //threaded operation + + nthreads = std::thread::hardware_concurrency(); + nthreads = (nthreads<1) ? 1 : nthreads; + nthreads = (nthreads>narray_max_threads) ? narray_max_threads : nthreads; + //if(nthreads<1) nthreads=1; + //if(nthreads>narray_max_threads) nthreads = narray_max_threads; + threads.resize(nthreads); + maxarr.resize(nthreads); + maxarr.setall(this->data[0]); + for(J=0;J, + this, + &maxarr, + J,nthreads + ); + } + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + ret = this->data[0]; + for(J=0;Jret) + { + ret = maxarr[J]; + } + } + } + return ret; + } + }; //end namespace narray }; //end namespace ams diff --git a/include/amscppnarray/amscppnarray_implsubarray.hpp b/include/amscppnarray/amscppnarray_implsubarray.hpp new file mode 100644 index 0000000..adfce90 --- /dev/null +++ b/include/amscppnarray/amscppnarray_implsubarray.hpp @@ -0,0 +1,44 @@ +#ifndef __AMSCPPNARRAY_IMPLSUBARRAY_HPP__ +#define __AMSCPPNARRAY_IMPLSUBARRAY_HPP__ + +namespace ams +{ +namespace narray +{ + + template narray narray::subarray(const narray_size_t ind1, const narray_size_t ind2) const + { + int res; + narray ret; + T defval = T(); + narray_size_t I,J; + + int nl = ind2-ind1; + + if(nl<=0) + { + ret.resize(0); + return ret; + } + + ret.resize(nl); + for(I=0;I=this->length) + { + ret.data[I] = defval; + } + else + { + ret.data[I] = this->data[J]; + } + } + + return ret; + } + +}; +}; + +#endif \ No newline at end of file diff --git a/include/amscppnarray/amscppnarray_random.hpp b/include/amscppnarray/amscppnarray_random.hpp new file mode 100644 index 0000000..75d6e76 --- /dev/null +++ b/include/amscppnarray/amscppnarray_random.hpp @@ -0,0 +1,12 @@ +#ifndef __AMSCPPNARRAY_RANDOM_HPP__ +#define __AMSCPPNARRAY_RANDOM_HPP__ + +namespace ams +{ +namespace narray +{ + +}; +}; + +#endif \ No newline at end of file diff --git a/src/amscppnarray/amscppnarray.cpp b/src/amscppnarray/amscppnarray.cpp index 7458e9d..7e5826d 100644 --- a/src/amscppnarray/amscppnarray.cpp +++ b/src/amscppnarray/amscppnarray.cpp @@ -74,6 +74,7 @@ void test_narray2() } + void test_narray3() { int I; @@ -83,17 +84,30 @@ void test_narray3() b = {0,0.25,0.5,0.675,0.8675}; c.resize(5); c.setall(2.0f); - d = -a*(b+c); + d = -a*(b+c*2+2); for(I=0;I<5;I++) { printf("d[%d]=%1.3f\n",I,d[I]); } - printf("d==-a*(b+c):%d\n",d==(-a*(b+c))); + printf("d==-a*(b+c*2+2):%d\n",d==(-a*(b+c*2+2))); d[4] = 0.0f; - printf("d==-a*(b+c):%d\n",d==(-a*(b+c))); - + printf("d==-a*(b+c*2+2):%d\n",d==(-a*(b+c*2+2))); + d = d.applyfn(::sinf); + + for(I=0;I<5;I++) + { + printf("d[%d]=%1.3f\n",I,d[I]); + + } + + d = d.subarray(0,1); + for(I=0;I + +namespace ams +{ +namespace narray +{ + +void narray_linspacef_tf( + narray *in, + const float low, const float high, const narray_size_t N, + int threadnum, int nthreads +) +{ + int I0,I1,Is,I; + float g; + + Is = in->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1); + for(I=I0;Idata[I] = low*(1.0f-g) + high*g; + } +} + + +narray linspacef(const float low, const float high, narray_size_t N) +{ + narray ret; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + float g; + + if(N<=0) + { + N = 0; + } + ret.resize(N); + + if(ret.length==0) + { + return ret; + } + + if(ret.lengthnarray_max_threads) ? narray_max_threads : nthreads; + //if(nthreads<1) nthreads=1; + //if(nthreads>narray_max_threads) nthreads = narray_max_threads; + threads.resize(nthreads); + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + + + + return ret; +} + +void narray_linspace_tf( + narray *in, + const double low, const double high, const narray_size_t N, + int threadnum, int nthreads +) +{ + int I0,I1,Is,I; + double g; + + Is = in->length/nthreads; + I0 = Is*threadnum; + I1 = (threadnum>=(nthreads-1))? in->length: Is*(threadnum+1); + for(I=I0;Idata[I] = low*(1.0-g) + high*g; + } +} + +narray linspace(const double low, const double high, narray_size_t N) +{ + narray ret; + narray_size_t I; + int J; + int nthreads; + std::vector threads; + double g; + + if(N<=0) + { + N = 0; + } + ret.resize(N); + + if(ret.length==0) + { + return ret; + } + + if(ret.lengthnarray_max_threads) ? narray_max_threads : nthreads; + //if(nthreads<1) nthreads=1; + //if(nthreads>narray_max_threads) nthreads = narray_max_threads; + threads.resize(nthreads); + for(J=0;Jjoin(); + delete threads[J]; + threads[J]= NULL; + } + } + } + + + + return ret; +} + + +}; //end namespaces +}; \ No newline at end of file diff --git a/src/amscppnarray/amscppnarray_random.cpp b/src/amscppnarray/amscppnarray_random.cpp new file mode 100644 index 0000000..a746ed0 --- /dev/null +++ b/src/amscppnarray/amscppnarray_random.cpp @@ -0,0 +1,11 @@ +#include + +namespace ams +{ +namespace narray +{ + + + +}; //end namespaces +}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index fc25301..2e1ba97 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ int main(int argc, char* argv[]) printf("ams c++ numeric array library tests.\n"); //ams::narray::test_narray1(); //ams::narray::test_narray2(); - ams::narray::test_narray3(); + //ams::narray::test_narray3(); return ret; } \ No newline at end of file