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