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
|
// Copyright (C) 2005 Anders Logg.
// Licensed under the GNU LGPL Version 2.1.
//
// First added: 2005-04-01
// Last changed: 2005
#ifndef __ECONOMY_H
#define __ECONOMY_H
#include <dolfin.h>
using namespace dolfin;
/// Base class for economies
class Economy : public Homotopy
{
public:
Economy(unsigned int m, unsigned int n) :
Homotopy(n), m(m), n(n), a(0), w(0),
tmp0(0), tmp1(0), tmp2(0), tmp3(0)
{
a = new real * [m];
w = new real * [m];
for (unsigned int i = 0; i < m; i++)
{
a[i] = new real[n];
w[i] = new real[n];
for (unsigned int j = 0; j < n; j++)
{
a[i][j] = dolfin::rand();
w[i][j] = dolfin::rand();
}
}
}
~Economy()
{
for (unsigned int i = 0; i < m; i++)
{
delete [] a[i];
delete [] w[i];
}
delete [] a;
delete [] w;
if ( tmp0 ) delete [] tmp0;
if ( tmp1 ) delete [] tmp1;
if ( tmp2 ) delete [] tmp2;
if ( tmp3 ) delete [] tmp3;
}
void disp()
{
cout << "Trader preferences:" << endl;
for (unsigned int i = 0; i < m; i++)
{
cout << " trader " << i << ":";
for (unsigned int j = 0; j < n; j++)
cout << " " << a[i][j];
cout << endl;
}
cout << "Initial endowments:" << endl;
for (unsigned int i = 0; i < m; i++)
{
cout << " trader " << i << ":";
for (unsigned int j = 0; j < n; j++)
cout << " " << w[i][j];
cout << endl;
}
}
// Number of traders
unsigned int m;
// Number of goods
unsigned int n;
// Matrix of traders' preferences
real** a;
// Matrix of traders' initial endowments
real** w;
protected:
// Compute sum of elements
complex sum(const complex x[]) const
{
complex sum = 0.0;
for (unsigned int j = 0; j < n; j++)
sum += x[j];
return sum;
}
// Compute sum of elements
complex bsum(const complex x[], unsigned int b) const
{
complex sum = 0.0;
for (unsigned int j = 0; j < n; j++)
sum += std::pow(x[j], b);
return sum;
}
// Compute scalar product x . y
complex dot(const real x[], const complex y[]) const
{
complex sum = 0.0;
for (unsigned int j = 0; j < n; j++)
sum += x[j] * y[j];
return sum;
}
// Compute special scalar product x . y.^b
complex bdot(const real x[], const complex y[], unsigned int b) const
{
complex sum = 0.0;
for (unsigned int j = 0; j < n; j++)
sum += x[j] * std::pow(y[j], b);
return sum;
}
// Compute special scalar product x . y.^b
complex bdot(const real x[], const complex y[], real b) const
{
complex sum = 0.0;
for (unsigned int j = 0; j < n; j++)
sum += x[j] * std::pow(y[j], b);
return sum;
}
// Compute special scalar product x . y.^b
complex bdot(const complex x[], const complex y[], unsigned int b) const
{
complex sum = 0.0;
for (unsigned int j = 0; j < n; j++)
sum += x[j] * std::pow(y[j], b);
return sum;
}
// Compute special scalar product x . y. z^b
complex bdot(const real x[], const complex y[], const complex z[], unsigned int b) const
{
complex sum = 0.0;
for (unsigned int j = 0; j < n; j++)
sum += x[j] * y[j] * std::pow(z[j], b);
return sum;
}
// Compute special scalar product x . y. z^b
complex bdot(const real x[], const complex y[], const complex z[], real b) const
{
complex sum = 0.0;
for (unsigned int j = 0; j < n; j++)
sum += x[j] * y[j] * std::pow(z[j], b);
return sum;
}
// Display values
void disp(const real x[], const char* name)
{
dolfin::cout << name << " = [";
for (unsigned int j = 0; j < n; j++)
dolfin::cout << x[j] << " ";
dolfin::cout << "]" << endl;
}
// Display values
void disp(const complex z[], const char* name)
{
dolfin::cout << name << " = [";
for (unsigned int j = 0; j < n; j++)
dolfin::cout << z[j] << " ";
dolfin::cout << "]" << endl;
}
// Initialize temporary storage for scalar products
void init(complex** tmp)
{
*tmp = new complex[m];
for (unsigned int i = 0; i < m; i++)
(*tmp)[i] = 0.0;
}
complex* tmp0; // Temporary storage for scalar products
complex* tmp1; // Temporary storage for scalar products
complex* tmp2; // Temporary storage for scalar products
complex* tmp3; // Temporary storage for scalar products
};
#endif
|