CASM  1.1.0
A Clusters Approach to Statistical Mechanics
BasisFunction.cc
Go to the documentation of this file.
2 
5 
6 namespace CASM {
7 
9 Array<Array<InnerProduct *> > Function::inner_prod_table =
10  Array<Array<InnerProduct *> >();
11 Array<Array<FunctionOperation *> > Function::operation_table =
12  Array<Array<FunctionOperation *> >();
13 
14 //*******************************************************************************************
15 
16 Function::Function(const std::vector<std::shared_ptr<BasisSet> > &_args)
17  : func_ID(ID_count++), m_argument(_args) {
18  // Index linear_ind(0);
19  for (Index i = 0; i < m_argument.size(); i++) {
20  for (Index j = 0; j < m_argument[i]->size(); j++) {
21  m_arg2sub.push_back(i);
22  m_arg2fun.push_back(j);
23  }
24  }
25 }
26 //*******************************************************************************************
27 
28 double Function::dot(Function const *RHS) const {
29  return inner_prod_table[this->class_ID()][RHS->class_ID()]->dot(this, RHS);
30 }
31 
32 //*******************************************************************************************
33 void Function::normalize() {
34  double mag(sqrt(this->dot(this)));
35  if (almost_zero(mag)) {
36  return;
37  }
38  m_formula.clear();
39  m_tex_formula.clear();
40  this->scale(1.0 / mag);
41 }
42 
43 //*******************************************************************************************
44 
45 bool Function::shallow_compare(Function const *RHS) const {
46  return (operation_table[this->class_ID()][RHS->class_ID()])
47  ->compare(this, RHS);
48 }
49 
50 //*******************************************************************************************
51 
52 bool Function::compare(Function const *RHS) const {
53  if (m_argument.size() != (RHS->m_argument).size()) return false;
54 
55  // if operations between this and RHS are undefined, return false
56  if (!operation_table[this->class_ID()][RHS->class_ID()]) return false;
57 
58  for (Index i = 0; i < m_argument.size(); i++) {
59  if (!(m_argument[i]->compare(*(RHS->m_argument)[i]))) return false;
60  }
61 
62  return shallow_compare(RHS);
63 }
64 
65 //*******************************************************************************************
66 
67 Function *Function::plus(Function const *RHS) const {
68  return operation_table[this->class_ID()][RHS->class_ID()]->add(this, RHS);
69 }
70 //*******************************************************************************************
71 
72 Function *Function::minus(Function const *RHS) const {
73  return operation_table[this->class_ID()][RHS->class_ID()]->subtract(this,
74  RHS);
75 }
76 //*******************************************************************************************
77 
78 Function *Function::poly_quotient(Function const *RHS) const {
79  return operation_table[this->class_ID()][RHS->class_ID()]->poly_quotient(this,
80  RHS);
81 }
82 //*******************************************************************************************
83 
84 Function *Function::poly_remainder(Function const *RHS) const {
85  return operation_table[this->class_ID()][RHS->class_ID()]->poly_remainder(
86  this, RHS);
87 }
88 //*******************************************************************************************
89 
90 Function *Function::multiply(Function const *RHS) const {
91  return (operation_table[this->class_ID()][RHS->class_ID()])
92  ->multiply(this, RHS);
93 }
94 //*******************************************************************************************
95 
96 Function *Function::plus_in_place(Function const *RHS) {
97  m_formula.clear();
98  m_tex_formula.clear();
99  refresh_ID();
100  return operation_table[this->class_ID()][RHS->class_ID()]->add_to(this, RHS);
101 }
102 //*******************************************************************************************
103 
104 Function *Function::minus_in_place(Function const *RHS) {
105  m_formula.clear();
106  m_tex_formula.clear();
107  refresh_ID();
108  return operation_table[this->class_ID()][RHS->class_ID()]->subtract_from(this,
109  RHS);
110 }
111 
112 //*******************************************************************************************
113 void Function::set_arguments(const ArgumentContainer &new_arg,
114  std::vector<Index> const &compatibility_map) {
115  _set_arguments(new_arg, compatibility_map);
116 
117  m_arg2sub.clear();
118  m_arg2fun.clear();
119  for (Index i = 0; i < new_arg.size(); i++) {
120  for (Index j = 0; j < new_arg[i]->size(); j++) {
121  m_arg2sub.push_back(i);
122  m_arg2fun.push_back(j);
123  }
124  }
125 }
126 //*******************************************************************************************
127 
128 ReturnArray<SymGroupRepID> Function::_sub_sym_reps() const {
129  Array<SymGroupRepID> t_result(m_argument.size());
130  for (Index i = 0; i < m_argument.size(); i++) {
131  t_result[i] = m_argument[i]->basis_symrep_ID();
132  }
133  return t_result;
134 }
135 
136 //*******************************************************************************************
137 
138 Function const *Function::_argument(Index i) const {
139  return (*m_argument[m_arg2sub[i]])[m_arg2fun[i]];
140 }
141 
142 //*******************************************************************************************
143 
144 double Function::_arg_eval_cache(Index i) const {
145  return m_argument[m_arg2sub[i]]->eval_cache(m_arg2fun[i]);
146 }
147 
148 //*******************************************************************************************
149 
150 double Function::_arg_deval_cache(Index i) const {
151  return m_argument[m_arg2sub[i]]->deval_cache(m_arg2fun[i]);
152 }
153 
154 //*******************************************************************************************
155 
156 int Function::_dependency_layer() const {
157  int tdep = -1;
158  for (Index i = 0; i < m_argument.size(); i++) {
159  tdep = max(tdep, m_argument[i]->dependency_layer());
160  }
161  return ++tdep;
162 }
163 //*******************************************************************************************
164 /*
165  Function *Function::_argument(Index i) {
166  return (*m_argument[m_arg2sub[i]])[m_arg2fun[i]];
167  }
168 */
169 //*******************************************************************************************
170 
171 bool Function::accept(const FunctionVisitor &visitor,
172  BasisSet const *home_basis_ptr) {
173  // bool is_updated(false);
174  // std::cout << "INSIDE BASE Function::accept\n";
175  // shared_basis
176  // for(Index i = 0; i < m_argument.size(); i++)
177  // is_updated = (m_argument[i]->accept(visitor)) || is_updated; // should we
178  // add && depends_on to first part?
179 
180  // if(is_updated) {
181  // m_formula.clear();
182  // m_tex_formula.clear();
183  //}
184  return _accept(visitor, home_basis_ptr); // || is_updated;
185 }
186 
187 //*******************************************************************************************
188 
189 bool Function::accept(const FunctionVisitor &visitor,
190  BasisSet const *home_basis_ptr) const {
191  // bool is_updated(false);
192  // std::cout << "INSIDE BASE Function::accept\n";
193  // shared_basis
194  // for(Index i = 0; i < m_argument.size(); i++)
195  // is_updated = (m_argument[i]->accept(visitor)) || is_updated; // should we
196  // add && depends_on to first part?
197 
198  // if(is_updated) {
199  // m_formula.clear();
200  // m_tex_formula.clear();
201  //}
202  return _accept(visitor, home_basis_ptr); // || is_updated;
203 }
204 
205 //*******************************************************************************************
206 
207 void Function::refresh_ID() { func_ID = ID_count++; }
208 
209 //*******************************************************************************************
210 
211 void Function::print(std::ostream &stream) const {
212  if (!m_formula.size()) {
213  this->make_formula();
214  }
215  stream << m_formula;
216  return;
217 }
218 //*******************************************************************************************
219 
220 void Function::print_tex(std::ostream &stream) const {
221  if (!m_formula.size()) {
222  this->make_formula();
223  }
224  stream << m_tex_formula;
225  return;
226 }
227 
228 //*******************************************************************************************
229 
230 Function *Function::sym_copy_coeffs(const SymOp &op,
231  int dependency_level) const {
232  Function *tptr(this->copy());
233  return tptr->apply_sym_coeffs(op, dependency_level);
234 }
235 
236 //*******************************************************************************************
237 
238 int Function::register_remotes(
239  const std::vector<DoF::RemoteHandle> &remote_handles) {
240  int t_tot(0);
241  // From now on, this will be handled at BasisSet level
242  // for(Index i = 0; i < m_argument.size(); i++) {
243  // t_tot += m_argument[i]->register_remotes(dof_name, remote_handles);
244  //}
245  return t_tot;
246 }
247 
248 //*******************************************************************************************
249 
250 bool Function::update_dof_IDs(const std::vector<Index> &before_IDs,
251  const std::vector<Index> &after_IDs) {
252  // JCT shared_basis
253  /*bool is_updated(false);
254  for(Index i = 0; i < m_argument.size(); i++) {
255  is_updated = ((m_argument[i]->update_dof_IDs(before_IDs, after_IDs)) &&
256  depends_on(m_argument[i])) || is_updated;
257  }
258 
259  if(is_updated) {
260  m_formula.clear();
261  m_tex_formula.clear();
262  }
263  */
264  return _update_dof_IDs(before_IDs, after_IDs);
265 }
266 
267 //*******************************************************************************************
268 void Function::set_identifier(char _key, std::string const &_value) {
269  m_identifiers[_key] = _value;
270 }
271 
272 //*******************************************************************************************
273 std::string Function::identifier(char _key) const {
274  auto it = m_identifiers.find(_key);
275  if (it == m_identifiers.end()) return "?";
276  return it->second;
277 }
278 
279 //*******************************************************************************************
280 
281 std::string Function::formula() const {
282  if (!m_formula.size()) {
283  this->make_formula();
284  }
285 
286  return m_formula;
287 }
288 
289 //*******************************************************************************************
290 
291 std::string Function::tex_formula() const {
292  if (!m_formula.size()) {
293  this->make_formula();
294  }
295 
296  return m_tex_formula;
297 }
298 
299 //*******************************************************************************************
300 
301 // Comparison
303  Function const *RHS) const {
304  // default behavior if Functions are of different types
305  return false;
306 }
307 
308 //*******************************************************************************************
309 
310 // Multiplication (tensor product)
311 Function *FunctionOperation::multiply(Function const *LHS,
312  Function const *RHS) const {
313  std::cerr << "WARNING: Multiplication of type " << LHS->type_name()
314  << " with type " << RHS->type_name() << " is undefined!!!\n";
315  return nullptr;
316 }
317 
318 //*******************************************************************************************
319 
320 Function *FunctionOperation::multiply_by(Function *LHS,
321  Function const *RHS) const {
322  std::cerr << "WARNING: Multiplication of type " << LHS->type_name()
323  << " with type " << RHS->type_name() << " is undefined!!!\n";
324  return nullptr;
325 }
326 
327 //*******************************************************************************************
328 
329 // Addition
330 Function *FunctionOperation::add(Function const *LHS,
331  Function const *RHS) const {
332  std::cerr << "WARNING: Addition of type " << LHS->type_name() << " with type "
333  << RHS->type_name() << " is undefined!!!\n";
334  return nullptr;
335 }
336 
337 //*******************************************************************************************
338 
339 Function *FunctionOperation::add_to(Function *LHS, Function const *RHS) const {
340  std::cerr << "WARNING: Addition of type " << LHS->type_name() << " with type "
341  << RHS->type_name() << " is undefined!!!\n";
342  return nullptr;
343 }
344 
345 //*******************************************************************************************
346 
347 // Subtraction
348 Function *FunctionOperation::subtract(Function const *LHS,
349  Function const *RHS) const {
350  std::cerr << "WARNING: Subtraction of type " << LHS->type_name()
351  << " with type " << RHS->type_name() << " is undefined!!!\n";
352  return nullptr;
353 }
354 
355 //*******************************************************************************************
356 
357 Function *FunctionOperation::subtract_from(Function *LHS,
358  Function const *RHS) const {
359  std::cerr << "WARNING: Subtraction of type " << LHS->type_name()
360  << " with type " << RHS->type_name() << " is undefined!!!\n";
361  return nullptr;
362 }
363 
364 //*******************************************************************************************
365 
366 // Polynomial division
367 Function *FunctionOperation::poly_quotient(Function const *LHS,
368  Function const *RHS) const {
369  std::cerr << "WARNING: Polynomial division of type " << LHS->type_name()
370  << " with type " << RHS->type_name() << " is undefined!!!\n";
371  return nullptr;
372 }
373 
374 //*******************************************************************************************
375 
376 Function *FunctionOperation::poly_remainder(Function const *LHS,
377  Function const *RHS) const {
378  std::cerr << "WARNING: Polynomial division of type " << LHS->type_name()
379  << " with type " << RHS->type_name() << " is undefined!!!\n";
380  return nullptr;
381 }
382 
383 //*******************************************************************************************
384 //** jsonParser stuff - Function
385 //*******************************************************************************************
386 
388  // Not quite sure what to do with all this. For now, just print 'formula'.
389 
390  // static members:
391  //
392  // class Function : public HierarchyID<Function>
393  // static int Nclass;
394  // static int ID_count;
395  // static Array< Array< InnerProduct * > > inner_prod_table;
396  // static Array< Array< TensorProduct * > > tensor_prod_table;
397  // static Array< Array< FunctionDifference * > > difference_table;
398  // static Array< Array< FunctionSum * > > sum_table;
399 
400  // int func_ID;
401  // Array<Function *> m_argument;
402  // mutable std::string m_formula, m_tex_formula;
403 
404  json.put_obj();
405 
406  json["m_formula"] = m_formula;
407 
408  return json;
409 }
410 
411 //*******************************************************************************************
412 
413 /*
414 void Function::from_json(const jsonParser &json) {
415 
416  // no reading functions for now
417 
418 }
419 */
420 
421 //*******************************************************************************************
422 
423 jsonParser &to_json(const Function *func, jsonParser &json) {
424  return func->to_json(json);
425 }
426 
427 // This does not exist: void from_json(Function *func, const jsonParser &json);
428 // Use the json (i.e. json["Function_type"] = "MonomialFunction")
429 // to know which Function's from_json to call
430 
431 } // namespace CASM
Basic std::vector like container (deprecated)
Definition: Array.hh:45
Function * poly_remainder(Function const *RHS) const
virtual std::string type_name() const =0
static Index ID_count
ArgumentContainer m_argument
virtual jsonParser & to_json(jsonParser &json) const
virtual Function * apply_sym_coeffs(const SymOp &op, int dependency_layer=1)
std::vector< std::shared_ptr< BasisSet > > ArgumentContainer
static Array< Array< InnerProduct * > > inner_prod_table
Function * poly_quotient(Function const *RHS) const
virtual int class_ID() const =0
static Array< Array< FunctionOperation * > > operation_table
Function * multiply(Function const *RHS) const
SymOp is the Coordinate representation of a symmetry operation it keeps fraction (FRAC) and Cartesian...
Definition: SymOp.hh:28
jsonParser & put_obj()
Puts new empty JSON object.
Definition: jsonParser.hh:354
T dot(const Tensor< T > &LHS, const Tensor< T > &RHS)
Definition: Tensor.hh:925
Main CASM namespace.
Definition: APICommand.hh:8
jsonParser & to_json(const ClexDescription &desc, jsonParser &json)
bool compare(ClusterInvariants const &A, ClusterInvariants const &B, double tol)
Compare ClusterInvariants.
INDEX_TYPE Index
For long integer indexing:
Definition: definitions.hh:39
T max(const T &A, const T &B)
Definition: CASM_math.hh:95
bool almost_zero(const MatrixBase< Derived > &val, double tol=CASM::TOL)
Round Eigen::MatrixXd.