BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
array.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2023.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#include "array.h"
20
21#include "sym_table.h"
22
23#include <iostream>
24
25using namespace GMAD;
26
28{
29}
30
32{
33 for(double it : array->array)
34 {
35 data.push_back(it);
36 }
37}
38
39Array::Array(unsigned int n)
40{
41 data.resize(n);
42}
43
45{
46 unsigned int size = std::min(a1->data.size(),a2->data.size());
47 Array* a = new Array(size);
48 for(unsigned int i=0;i<size;i++)
49 {
50 a->data[i] = a1->data[i] + a2->data[i];
51 }
52 return a;
53}
54
56{
57 unsigned int size = std::min(a1->data.size(),a2->data.size());
58 Array* a = new Array(size);
59 for(unsigned int i=0;i<size;i++)
60 {
61 a->data[i] = a1->data[i] - a2->data[i];
62 }
63 return a;
64}
65
66Array* Array::Add(Array* a1, double d2)
67{
68 unsigned int size = a1->data.size();
69 Array* a = new Array(size);
70 for(unsigned int i=0;i<size;i++)
71 {
72 a->data[i] = a1->data[i] + d2;
73 }
74 return a;
75}
76
77Array* Array::Subtract(Array* a1, double d2)
78{
79 unsigned int size = a1->data.size();
80 Array* a = new Array(size);
81 for(unsigned int i=0;i<size;i++)
82 {
83 a->data[i] = a1->data[i] - d2;
84 }
85 return a;
86}
87
88Array* Array::Multiply(Array* a1, double d2)
89{
90 unsigned int size = a1->data.size();
91 Array* a = new Array(size);
92 for(unsigned int i=0;i<size;i++)
93 {
94 a->data[i] = a1->data[i] * d2;
95 }
96 return a;
97}
98
99Array* Array::Divide(Array* a1, double d2)
100{
101 unsigned int size = a1->data.size();
102 Array* a = new Array(size);
103 for(unsigned int i=0;i<size;i++)
104 {
105 a->data[i] = a1->data[i] / d2;
106 }
107 return a;
108}
109
111{
112 if(data.size() != a->data.size())
113 {
114 std::cerr << "ERROR: vector dimensions do not match" << std::endl;
115 exit(1);
116 }
117
118 double dummy = 0;
119 for(unsigned int i=0;i<data.size();i++)
120 dummy += data[i] * a->data[i];
121 return dummy;
122}
123
125{
126 data.clear();
127 symbols.clear();
128}
129
131{
132 for(double d : data)
133 {
134 std::cout << d << " ";
135 }
136 std::cout << std::endl;
137 for(std::string s : symbols)
138 {
139 std::cout << s << " ";
140 }
141 std::cout << std::endl;
142}
143
144std::list<std::string> Array::GetSymbolsList() const
145{
146 std::list<std::string> r;
147 for (const std::string& s : symbols)
148 {r.push_back(s);}
149 return r;
150}
151
152std::list<double> Array::GetDataList() const
153{
154 std::list<double> r;
155 for (double d : data)
156 {r.push_back(d);}
157 return r;
158}
Representation of arrays used in tokens.
Definition: array.h:40
static Array * Subtract(Array *a1, Array *a2)
Constructor from subtracting 2 double arrays.
Definition: array.cc:55
static Array * Multiply(Array *a, double d)
Constructor from multiplying an array.
Definition: array.cc:88
std::vector< std::string > symbols
Representation of string array.
Definition: array.h:44
std::vector< double > data
Representation of double array.
Definition: array.h:46
static Array * Divide(Array *a, double d)
Constructor from dividing an array.
Definition: array.cc:99
void Clear()
Clear data.
Definition: array.cc:124
static Array * Add(Array *a1, Array *a2)
Constructor from adding 2 double arrays.
Definition: array.cc:44
double Product(Array *a)
Scalar vector product.
Definition: array.cc:110
Array()
Default Constructor.
Definition: array.cc:27
void Print()
Print data.
Definition: array.cc:130
Common header for the lexer and the parser to share Symbol table for numeric variables,...
Definition: sym_table.h:33
std::list< double > array
data
Definition: sym_table.h:88
Parser namespace for GMAD language. Combination of Geant4 and MAD.