Cytnx v0.7.6
Loading...
Searching...
No Matches
stat.hpp
Go to the documentation of this file.
1#ifndef _stat_H_
2#define _stat_H_
3
4#include "Type.hpp"
5#include "cytnx_error.hpp"
6#include "Storage.hpp"
7#include "Tensor.hpp"
8#include <algorithm>
9#include <iostream>
10
11
12
13
14namespace cytnx{
15 namespace stat{
16
18 class Histogram{
19 public:
20 double min;
21 double max;
22 uint64_t bins;
25
26 //std::vector<double> vars;
27 //std::vector<double> x;
28
30
31
35 Histogram(const unsigned long long &Nbins, const double &min_val, const double &max_val);
36
38 Histogram(const Histogram &rhs){
39 this->min = rhs.min;
40 this->max = rhs.max;
41 this->bins = rhs.bins;
42 this->vars = rhs.vars.clone();
43 this->x = rhs.x.clone();
44 this->total_count = rhs.total_count;
45 }
46
47 Histogram& operator=(const Histogram &rhs){
48 this->min = rhs.min;
49 this->max = rhs.max;
50 this->bins = rhs.bins;
51 this->vars = rhs.vars.clone();
52 this->x = rhs.x.clone();
53 this->total_count = rhs.total_count;
54 return *this;
55 }
57
58 void clear_vars(){
59 total_count = 0;
60 memset(this->vars.data(),0,sizeof(double)*this->vars.size());
61 }
62
63 template<class T>
64 void accumulate(const std::vector<T> &data){
65 std::vector<T> tmp = data;
66 std::sort(tmp.begin(),tmp.end());
67
68 uint64_t cntr = 0;
69 double curr_x = 0;
70 double dx = double(max-min)/bins;
71
72 // for each elem in data, compare
73 for(unsigned long long i=0;i<tmp.size();i++){
74 while(cntr<=this->bins){
75 if(tmp[i] < curr_x){
76 if(cntr){ vars.at<double>(cntr-1) += 1; total_count+=1;}
77 break;
78 }
79 cntr++;
80 curr_x = cntr*dx;
81 }
82 }
83 }
84
85 void normalize();
86 void print() const;
87
88 const Storage& get_x()const{
89 //get x
90 return this->x;
91 }
93 //get x
94 return this->x;
95 }
96
98 return this->x.size();
99 }
100
101
102 };//class histogram
103
106 public:
107 double minx,miny;
108 double maxx,maxy;
109 uint64_t binx,biny;
113
114 //std::vector<double> vars;
115 //std::vector<double> x;
116
118
119
123 Histogram2d(const unsigned long long &Nbinx, const unsigned long long &Nbiny, const double &min_x, const double &max_x, const double &min_y, const double &max_y);
124
126 Histogram2d(const Histogram2d &rhs){
127 this->minx = rhs.minx;
128 this->maxx = rhs.maxx;
129 this->miny = rhs.miny;
130 this->maxy = rhs.maxy;
131 this->binx = rhs.binx;
132 this->biny = rhs.biny;
133 this->vars = rhs.vars.clone();
134 this->x = rhs.x.clone();
135 this->y = rhs.y.clone();
136 this->total_count = rhs.total_count;
137 }
138
139 Histogram2d& operator=(const Histogram2d &rhs){
140 this->minx = rhs.minx;
141 this->maxx = rhs.maxx;
142 this->miny = rhs.miny;
143 this->maxy = rhs.maxy;
144 this->binx = rhs.binx;
145 this->biny = rhs.biny;
146 this->vars = rhs.vars.clone();
147 this->x = rhs.x.clone();
148 this->y = rhs.y.clone();
149 this->total_count = rhs.total_count;
150 return *this;
151 }
153
155 total_count = 0;
156 memset(this->vars.data(),0,sizeof(double)*this->vars.size());
157 }
158
159 template<class T>
160 void accumulate(const std::vector<T> &data_x,const std::vector<T> &data_y){
161 //[not fin!]
162 cytnx_error_msg(data_x.size()!=data_y.size(), "[ERROR][Histogram2d::accumulate] data_x and data_y should have same size!%s","\n");
163
164 double dx = double(maxx-minx)/binx;
165 double dy = double(maxy-miny)/biny;
166
167 unsigned int nx,ny;
168
169 // for each elem in data, compare
170 for(unsigned long long i=0;i<data_x.size();i++){
171
172 nx = floor(data_x[i]/dx);
173 ny = floor(data_y[i]/dy);
174 vars.at<double>(ny*binx+nx) +=1; total_count+=1;
175
176 }
177 }
178
179 void normalize();
180 void print() const;
181
182 const Storage& get_x()const{
183 //get x
184 return this->x;
185 }
187 //get x
188 return this->x;
189 }
190
191 const Storage& get_y()const{
192 //get y
193 return this->y;
194 }
196 //get y
197 return this->y;
198 }
199
200 std::vector<cytnx_uint64> size(){
201 return std::vector<cytnx_uint64>({this->x.size(),this->y.size()});
202 }
203
204
205
206
207 };//class histogram
208
209
210 //function, statistical:
211 //class Boostrap{
212
213
214 //};//class bootstrap
215
216
217
218
219
220
221 }//stat
222}//cytnx
223
224
225#endif
an memeory storage with multi-type/multi-device support
Definition Storage.hpp:934
const unsigned long long & size() const
the size ( no. of elements ) in the Storage
Definition Storage.hpp:1201
Storage clone() const
return a copy of current storage.
Definition Storage.hpp:1192
2D, real value histogram
Definition stat.hpp:105
uint64_t binx
Definition stat.hpp:109
double minx
Definition stat.hpp:107
uint64_t biny
Definition stat.hpp:109
Storage & get_x()
Definition stat.hpp:186
double maxy
Definition stat.hpp:108
void clear_vars()
Definition stat.hpp:154
double miny
Definition stat.hpp:107
std::vector< cytnx_uint64 > size()
Definition stat.hpp:200
const Storage & get_x() const
Definition stat.hpp:182
Storage & get_y()
Definition stat.hpp:195
Histogram2d(const unsigned long long &Nbinx, const unsigned long long &Nbiny, const double &min_x, const double &max_x, const double &min_y, const double &max_y)
initialize a histogram
double total_count
Definition stat.hpp:117
cytnx::Storage y
Definition stat.hpp:112
cytnx::Storage vars
Definition stat.hpp:110
cytnx::Storage x
Definition stat.hpp:111
const Storage & get_y() const
Definition stat.hpp:191
void accumulate(const std::vector< T > &data_x, const std::vector< T > &data_y)
Definition stat.hpp:160
double maxx
Definition stat.hpp:108
1D, real value histogram
Definition stat.hpp:18
cytnx::Storage x
Definition stat.hpp:24
cytnx::Storage vars
Definition stat.hpp:23
void clear_vars()
Definition stat.hpp:58
const Storage & get_x() const
Definition stat.hpp:88
Histogram(const unsigned long long &Nbins, const double &min_val, const double &max_val)
initialize a histogram
uint64_t bins
Definition stat.hpp:22
double min
Definition stat.hpp:20
double total_count
Definition stat.hpp:29
void accumulate(const std::vector< T > &data)
Definition stat.hpp:64
cytnx_uint64 size()
Definition stat.hpp:97
double max
Definition stat.hpp:21
Storage & get_x()
Definition stat.hpp:92
#define cytnx_error_msg(is_true, format,...)
Definition cytnx_error.hpp:18
Definition Accessor.hpp:12
uint64_t cytnx_uint64
Definition Type.hpp:22