Dividing Variables into "bins"

Pages: 123
Aug 10, 2011 at 1:01am
Sorry about the confusion. I don't know what I was thinking.
Aug 10, 2011 at 1:10am
closed account (zbREy60M)
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
double f = hxrf;
unsigned histogram[650]={0};
double sums[650]={0};
std::vector<double> frequency_bins[650];

size_t index=size_t((f<1E-10)?0:log10(f)+10);
sums[index]+=compute_h_value(f);
frequency_bins[index].push_back(f);


//double f = hxrf;
//size_t index=size_t((f<1E-6)?0:log10(f)+6);
//histogram[index]++;
//std::cout <<"histogram["<<index<<"]="<<histogram[index]<<std::endl;


//printf("Gravitar # %4f %16f %16f %16f %16f %16f %32f %16f %16f %16f %16f \n" , R+1 ,  x , y , z , a , p , Ellipse , prefrequency , postfrequency, dge, h, hxrf );

}

for (int a=0;a<650;a++){
    std::cout <<"Bucket "<<a<<":\n"
        "Sum of h values for bucket: "<<sums[a]<<"\n"
        "Frequencies in bucket:\n";
    for (size_t b=0;b<frequency_bins[a].size();b++)
        std::cout <<frequency_bins[a][b]<<" ";
    std::cout <<std::endl;
}


Okay. I plugged that stuff in and I'm getting some errors. I think we may have different libraries or something...

I'm getting an error that says "compute_h_value" identifier not found
The same with "sums" and "frequency_bins"

It says "left of '.size' must have a class union structure.
Aug 10, 2011 at 1:16am
1. #include <vector>
2. Make sure you placed your declarations where they belong.
3. Of course, you'll have to define what compute_h_value() means. I don't know what it is.
'h' is a variable dependent on the frequency
Aug 10, 2011 at 1:27am
closed account (zbREy60M)
Ah, h is pretty much frequency. The "hxrf" variable that I set "f" equal to is what we are throwing into bins. We don't need to modify it after this. So I just set it equal to f in the program.

Alright. I got the program to run, but we are having a similar problem. This time I am just getting

Bucket M:
Sum of h values for bucket: 0
Frequencies in bucket:

With M being the number of the bucket. there is always 0 total h values, and no frequencies are ever listed.

The h is probably just too small to be displayed. (how can i get it to display to more digits? or do arrays only hold ints?)

I'm not sure why the frequencies in buckets is not working. Here is my code:

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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <ctime>
#include <fstream>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
#include "boost/random.hpp"
#include "boost/generator_iterator.hpp"
#include "boost/random/normal_distribution.hpp"
#include <boost/random/uniform_int.hpp>
#include <boost/random/uniform_01.hpp>

using namespace std;
using std::vector;  

unsigned histogram[650]={0};
double sums[650]={0};
std::vector<double> frequency_bins[650];

class Gravitar //creates a class called Gravitar
{
public: //public assesors
long	double GetRadius();
	void SetRadius(long double radius);
long	double GetAngle();
	void SetAngle(long double angle);
long	double GetZed();
	void SetZed(long double zed);
long	double GetAge();
	void SetAge(long double age);
long	double GetPeriod();
	void SetPeriod(long double period);

private: //private values
long	double Radius;
long	double Angle;
long	double Zed;
long	double Age;
long	double Period;
};
long double Gravitar::GetRadius() //GetRadius, public assessor function returns value of Radius
{
	return Radius; //returns the value Radius
}
long double Gravitar::GetAngle()
{
	return Angle; //returns the value of Angle
}
long double Gravitar::GetZed() 
{
	return Zed;
}
long double Gravitar::GetAge()
{
	return Age;
}
long double Gravitar::GetPeriod()
{
	return Period;
}
void Gravitar::SetRadius(long double radius)//sets radius to Radius
{
	Radius = radius;
}
void Gravitar::SetAngle(long double angle)
{
	Angle = angle;
}
void Gravitar::SetZed(long double zed)
{
	Zed = zed;
}
void Gravitar::SetAge(long double age)
{
	Age = age;
}
void Gravitar::SetPeriod(long double period)
{
	Period = period;
}

int main()
{
int m ;
cout << "How many Gravitars?\n\n";
cin >> m;
long double e ;
cout << "\n\nWhat is the ellipticity of the objects? Enter 6, 7, 8, 9, for gravitars of ellipticity of 10^-x.\n\n";
cin >> e;

long double Ellipse =  pow (10, -e); //the ellipticity should vary from 10^-6 to 10^-7 to 10^-8 to 10^-9


	boost::mt19937 rng( time(0) );
	boost::normal_distribution<> nd(5, .69);
	boost::variate_generator<boost::mt19937&,  boost::normal_distribution<> > var_nor(rng, nd);
	boost::minstd_rand intgen;
	boost::uniform_01<boost::minstd_rand> gen(intgen);
  
long double R;

ofstream myfile
//("agedata.txt"); 
//("positiondata.txt");
("postspindowndata.txt");
//("postspindowndata.dat");

unsigned histogram[650]={0};
for (int R = 0; R < m; R++)
{	

long double radius = sqrt(gen());
long double angle = gen();
long double zed = gen();
long double age = gen();
long double period = var_nor()*.001; // normal distribution of periods with a mean of 5 ms and a standard deviation of .69 mseconds 

Gravitar RadialComponents;
RadialComponents.SetRadius(radius);
Gravitar AngularComponents;
AngularComponents.SetAngle(angle);
Gravitar ZedComponents;
ZedComponents.SetZed(zed);
Gravitar AgeComponents;
AgeComponents.SetAge(age);
Gravitar PeriodComponents;
PeriodComponents.SetPeriod(period);

long double x = RadialComponents.GetRadius()*cos (AngularComponents.GetAngle()*6.283185307179586476925286766559)*15329.729; 
long double y = RadialComponents.GetRadius()*sin (AngularComponents.GetAngle()*6.283185307179586476925286766559)*15329.729;
long double z = ZedComponents.GetZed()*153.29729;
long double a = AgeComponents.GetAge() * 10000000.0 * 31556926.0; // Random number generated between 0 and 1, then multiplied by 10 millions years, multiplied by the number of seconds within a year
long double p = PeriodComponents.GetPeriod(); //p measured in seconds
long double prefrequency = 2.0 / p;
long double xde = x;
long double deltay = (y - 8500);
long double deltay2 = deltay*deltay;
long double yde = sqrt(deltay2);
long double zde = z;
long double xde2 = xde*xde;
long double yde2 = yde*yde;
long double zde2 = zde*zde;
long double dge = sqrt(xde2 + yde2 + zde2);
//WARNING:E is the  NOT mathematical constant e.
//It is the const var of ellipticity
long double Ellipse2=Ellipse*Ellipse;
const long double pi=3.1415926535897932384626433832795,
	pi4=pi*pi*pi*pi,
	pi2=pi*pi,
	G=6.673E-11;
long double beta= (32.0 * pi4 * G * 10E+44) / (2.421E+42);
int value = -4;
long double othervalue = -.25;
long double postfrequency = pow( (pow(prefrequency, value) + beta * Ellipse2 * a), othervalue);
long double h = ((4.0 * pi2 * G * 10E+44) / (8.077E+33)) * ((Ellipse * pow( postfrequency, 2)) / dge);
long double hx = h*1000000000;
long double hxr = floor (hx);
double hxrf = hxr/1000000000;

double f = hxrf;

size_t index=size_t((f<1E-10)?0:log10(f)+10);
sums[index]+=f;
frequency_bins[index].push_back(f);

//double f = hxrf;
//size_t index=size_t((f<1E-6)?0:log10(f)+6);
//histogram[index]++;
//std::cout <<"histogram["<<index<<"]="<<histogram[index]<<std::endl;


//printf("Gravitar # %4f %16f %16f %16f %16f %16f %32f %16f %16f %16f %16f \n" , R+1 ,  x , y , z , a , p , Ellipse , prefrequency , postfrequency, dge, h, hxrf );

}

for (int a=0;a<650;a++){
    std::cout <<"Bucket "<<a<<":\n"
        "Sum of h values for bucket: "<<sums[a]<<"\n"
        "Frequencies in bucket:\n";
    for (size_t b=0;b<frequency_bins[a].size();b++)
        std::cout <<frequency_bins[a][b]<<" ";
    std::cout <<std::endl;
}


myfile.close();

system("PAUSE");
return 0;
}


Aug 10, 2011 at 1:43am
This works for me:
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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <ctime>
#include <fstream>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
#include "boost/random.hpp"
#include "boost/generator_iterator.hpp"
#include "boost/random/normal_distribution.hpp"
#include <boost/random/uniform_int.hpp>
#include <boost/random/uniform_01.hpp>

using namespace std;
using std::vector;

class Gravitar //creates a class called Gravitar
{
public: //public assesors
    long    double GetRadius();
    void SetRadius(long double radius);
    long    double GetAngle();
    void SetAngle(long double angle);
    long    double GetZed();
    void SetZed(long double zed);
    long    double GetAge();
    void SetAge(long double age);
    long    double GetPeriod();
    void SetPeriod(long double period);

private: //private values
    long    double Radius;
    long    double Angle;
    long    double Zed;
    long    double Age;
    long    double Period;
};

long double Gravitar::GetRadius() //GetRadius, public assessor function returns value of Radius
{
    return Radius; //returns the value Radius
}

long double Gravitar::GetAngle()
{
    return Angle; //returns the value of Angle
}

long double Gravitar::GetZed()
{
    return Zed;
}

long double Gravitar::GetAge()
{
    return Age;
}

long double Gravitar::GetPeriod()
{
    return Period;
}

void Gravitar::SetRadius(long double radius)//sets radius to Radius
{
    Radius = radius;
}

void Gravitar::SetAngle(long double angle)
{
    Angle = angle;
}

void Gravitar::SetZed(long double zed)
{
    Zed = zed;
}

void Gravitar::SetAge(long double age)
{
    Age = age;
}

void Gravitar::SetPeriod(long double period)
{
    Period = period;
}

int main()
{
    int m ;
    cout << "How many Gravitars?\n\n";
    cin >> m;
    long double e ;
    cout << "\n\nWhat is the ellipticity of the objects? Enter 6, 7, 8, 9, for gravitars of ellipticity of 10^-x.\n\n";
    cin >> e;

    long double Ellipse =  pow (10, -e); //the ellipticity should vary from 10^-6 to 10^-7 to 10^-8 to 10^-9


    boost::mt19937 rng( time(0) );
    boost::normal_distribution<> nd(5, .69);
    boost::variate_generator<boost::mt19937&,  boost::normal_distribution<> > var_nor(rng, nd);
    boost::minstd_rand intgen;
    boost::uniform_01<boost::minstd_rand> gen(intgen);

    ofstream myfile("postspindowndata.txt");

    double sums[650]={0};
    std::vector<double> frequency_bins[650];
    for (int R = 0; R < m; R++) {

        long double radius = sqrt(gen());
        long double angle = gen();
        long double zed = gen();
        long double age = gen();
        long double period = var_nor()*.001; // normal distribution of periods with a mean of 5 ms and a standard deviation of .69 mseconds

        Gravitar RadialComponents;
        RadialComponents.SetRadius(radius);
        Gravitar AngularComponents;
        AngularComponents.SetAngle(angle);
        Gravitar ZedComponents;
        ZedComponents.SetZed(zed);
        Gravitar AgeComponents;
        AgeComponents.SetAge(age);
        Gravitar PeriodComponents;
        PeriodComponents.SetPeriod(period);

        long double x = RadialComponents.GetRadius()*cos (AngularComponents.GetAngle()*6.283185307179586476925286766559)*15329.729;
        long double y = RadialComponents.GetRadius()*sin (AngularComponents.GetAngle()*6.283185307179586476925286766559)*15329.729;
        long double z = ZedComponents.GetZed()*153.29729;
        long double a = AgeComponents.GetAge() * 10000000.0 * 31556926.0; // Random number generated between 0 and 1, then multiplied by 10 millions years, multiplied by the number of seconds within a year
        long double p = PeriodComponents.GetPeriod(); //p measured in seconds
        long double prefrequency = 2.0 / p;
        long double xde = x;
        long double deltay = (y - 8500);
        long double deltay2 = deltay*deltay;
        long double yde = sqrt(deltay2);
        long double zde = z;
        long double xde2 = xde*xde;
        long double yde2 = yde*yde;
        long double zde2 = zde*zde;
        long double dge = sqrt(xde2 + yde2 + zde2);
//WARNING:E is the  NOT mathematical constant e.
//It is the const var of ellipticity
        long double Ellipse2=Ellipse*Ellipse;
        const long double pi=3.1415926535897932384626433832795,
                          pi4=pi*pi*pi*pi,
                          pi2=pi*pi,
                          G=6.673E-11;
        long double beta= (32.0 * pi4 * G * 10E+44) / (2.421E+42);
        int value = -4;
        long double othervalue = -.25;
        long double postfrequency = pow( (pow(prefrequency, value) + beta * Ellipse2 * a), othervalue);
        long double h = ((4.0 * pi2 * G * 10E+44) / (8.077E+33)) * ((Ellipse * pow( postfrequency, 2)) / dge);
        long double hx = h*1000000000;
        long double hxr = floor (hx);
        double hxrf = hxr/1000000000;

        double f = hxrf;
        size_t index=size_t((f<1E-10)?0:log10(f)+10);
        sums[index]+=f;
        frequency_bins[index].push_back(f);


    }

    for (int a=0;a<650;a++){
        if (!frequency_bins[a].size())
            continue;
        std::cout <<"Bucket "<<a<<" ["<<(!a?0:pow((double)10,(double)a-10))<<";"<<pow((double)10,(double)a-9)<<"):\n"
            "Sum of h values for bucket: "<<sums[a]<<"\n"
            "Frequencies in bucket:\n";
        for (size_t b=0;b<frequency_bins[a].size();b++)
            std::cout <<frequency_bins[a][b]<<" ";
        std::cout <<std::endl;
    }

    myfile.close();

    system("PAUSE");
    return 0;
}
Last edited on Aug 10, 2011 at 1:44am
Aug 10, 2011 at 1:49am
closed account (zbREy60M)
Thats fantastic! Thanks! I'll see how this all works out! You've been extraordinary.
Aug 10, 2011 at 1:56am
closed account (zbREy60M)
Real quick: How would I get this to write inside of a file? It's just couting in the cmd window right now.
Aug 10, 2011 at 2:21am
Change std::cout to your file.
Aug 10, 2011 at 2:45am
closed account (zbREy60M)
Awesome, again. Thanks! We'll see if this is what I need.
Aug 10, 2011 at 5:44pm
closed account (zbREy60M)
Alright. I might need to form more buckets later. I need to be able to understand what the notation in this line says, so that I can change it at will, it still reads like greek to me

size_t index=size_t((f<1E-10)?0:log10(f)+10);

I get the first part of it, but once the ? starts, I have no idea. I tried messing around with it to try to modify boxes myself but I just keep getting memory errors.
Aug 10, 2011 at 6:19pm
As tition said in the previous page, the line is equivalent to
1
2
3
4
5
size_t index;
if (f<1E-10)
    index=0;
else
    index=size_t(log10(f)+10);


To shift the buckets around, you can make it more parametric:
1
2
const double k=10;
size_t index=size_t((f<pow(10,-k))?0:log10(f)+k);
If you keep the same logarithmic distribution, there's no use in adding more buckets.
Last edited on Aug 10, 2011 at 6:19pm
Aug 11, 2011 at 3:03am
closed account (zbREy60M)
Yeah, I'm thinking I'm going to have to change the logarithmic distribution. I was told to do it logarithmically, but I was also told to make a histogram, and the range of the frequencies is not large enough to make a histogram of any appreciable histogramminess.
Aug 13, 2011 at 5:25pm
closed account (zbREy60M)
How do I add more buckets? I've been screwing around with this line:

1
2
3
4
5
size_t index;
		if (f<1E-10)
			index=0;
		else
			index=size_t(((log10(f)))+10);
But It seems to just change the range of each of the bucket. I'm assuming I'm going to have to modify these lines:

1
2
3
4
5
6
7
8
9
for (int a=0;a<650;a++){
        if (!frequency_bins[a].size())
            continue;
        myfile <<"Bucket "<<a<<" ["<<(!a?0:pow((double)10,(double)a-10))<<";"<<pow((double)10,(double)a-9)<<"):\n"
            "Sum of h values for bucket: "<<sums[a]<<"\n"
            "h-values in bucket:\n";
        for (size_t b=0;b<frequency_bins[a].size();b++)
            myfile <<frequency_bins[a][b]<<" ";
        myfile <<std::endl;


But this is again written in the notation that I don't really understand. A quick pointer on how to change bucket size in this code would be greatly appreciated!
Aug 13, 2011 at 6:10pm
Do you still want it to be logarithmic? It seems most frequencies are clustered very close to zero, so I think equally-sized buckets may be more appropriate.
Aug 13, 2011 at 6:12pm
closed account (zbREy60M)
I need it to no longer be logarithmic. Evenly spaced buckets would be good!
Aug 13, 2011 at 6:54pm
1
2
3
4
5
6
if (f<min)
    index=0;
else if (f>=max)
    index=bucket_count-1;
else
    index=size_t((f-min)/((max-min)/double(bucket_count));
Where the range of frequencies you're interested in is [min;max), and you want bucket_count buckets. Note that in all the code I posted earlier, bucket_count==650.
Last edited on Aug 13, 2011 at 6:55pm
Topic archived. No new replies allowed.
Pages: 123