Skip to content

Commit 257b72f

Browse files
author
Dominic Price
committed
Optimise AdjformEx to use int32_t instead of mpq_class
1 parent 1799aa7 commit 257b72f

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

core/Adjform.cc

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -359,20 +359,22 @@ namespace cadabra {
359359
return -(Adjform::value_type)data->begin().number_of_children();
360360
}
361361

362-
AdjformEx::rational_type AdjformEx::zero = 0;
362+
AdjformEx::integer_type AdjformEx::zero = 0;
363363

364364
AdjformEx::AdjformEx()
365365
{
366366

367367
}
368368

369-
AdjformEx::AdjformEx(const Adjform& adjform, const AdjformEx::rational_type& value, const Ex& prefactor)
369+
AdjformEx::AdjformEx(const Adjform& adjform, const AdjformEx::integer_type& value, const Ex& prefactor)
370370
: prefactor(prefactor)
371371
{
372+
sizeof(mpq_class);
373+
sizeof(int64_t);
372374
set(adjform, value);
373375
}
374376

375-
AdjformEx::AdjformEx(const Adjform& adjform, const rational_type& value, Ex::iterator prefactor)
377+
AdjformEx::AdjformEx(const Adjform& adjform, const integer_type& value, Ex::iterator prefactor)
376378
: prefactor(prefactor)
377379
{
378380
set(adjform, value);
@@ -412,7 +414,7 @@ namespace cadabra {
412414
one(tensor_begin->multiplier);
413415
}
414416

415-
AdjformEx::rational_type AdjformEx::compare(const AdjformEx& other) const
417+
AdjformEx::integer_type AdjformEx::compare(const AdjformEx& other) const
416418
{
417419
// Early failure checks
418420
if (data.empty() || data.size() != other.data.size())
@@ -421,7 +423,7 @@ namespace cadabra {
421423
// Find the numeric factor between the first two terms, then loop over all
422424
// other terms checking that the factor is the same. If not, return 0
423425
auto a_it = data.begin(), b_it = other.data.begin(), a_end = data.end();
424-
rational_type factor = a_it->second / b_it->second;
426+
integer_type factor = a_it->second / b_it->second;
425427
while (a_it != a_end) {
426428
if (a_it->first != b_it->first)
427429
return 0;
@@ -438,13 +440,13 @@ namespace cadabra {
438440
add(kv.first, kv.second);
439441
}
440442

441-
void AdjformEx::combine(const AdjformEx& other, AdjformEx::rational_type factor)
443+
void AdjformEx::combine(const AdjformEx& other, AdjformEx::integer_type factor)
442444
{
443445
for (const auto& kv : other.data)
444446
add(kv.first, kv.second * factor);
445447
}
446448

447-
void AdjformEx::multiply(const AdjformEx::rational_type& k)
449+
void AdjformEx::multiply(const AdjformEx::integer_type& k)
448450
{
449451
for (auto& kv : data)
450452
kv.second *= k;
@@ -519,33 +521,33 @@ namespace cadabra {
519521
return tensor;
520522
}
521523

522-
const AdjformEx::rational_type& AdjformEx::get(const Adjform& adjform) const
524+
const AdjformEx::integer_type& AdjformEx::get(const Adjform& adjform) const
523525
{
524526
auto pos = data.find(adjform);
525527
return (pos == data.end()) ? zero : pos->second;
526528
}
527529

528-
void AdjformEx::set(const Adjform& term, const AdjformEx::rational_type& value)
530+
void AdjformEx::set(const Adjform& term, const AdjformEx::integer_type& value)
529531
{
530532
if (!term.empty())
531533
set_(term, value);
532534
}
533535

534-
void AdjformEx::set_(const Adjform& term, const AdjformEx::rational_type& value)
536+
void AdjformEx::set_(const Adjform& term, const AdjformEx::integer_type& value)
535537
{
536538
if (value != 0)
537539
data[term] = value;
538540
else
539541
data.erase(term);
540542
}
541543

542-
void AdjformEx::add(const Adjform& term, const AdjformEx::rational_type& value)
544+
void AdjformEx::add(const Adjform& term, const AdjformEx::integer_type& value)
543545
{
544546
if (!term.empty())
545547
add_(term, value);
546548
}
547549

548-
void AdjformEx::add_(const Adjform& term, const AdjformEx::rational_type& value)
550+
void AdjformEx::add_(const Adjform& term, const AdjformEx::integer_type& value)
549551
{
550552
auto elem = data.find(term);
551553
if (elem == data.end() && value != 0) {

core/Adjform.hh

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,23 @@ namespace cadabra {
8484
class AdjformEx
8585
{
8686
public:
87-
using rational_type = mpq_class;
88-
using map_t = std::map<Adjform, rational_type>;
87+
using integer_type = int32_t;
88+
using map_t = std::map<Adjform, integer_type>;
8989
using iterator = map_t::iterator;
9090
using const_iterator = map_t::const_iterator;
9191

9292
AdjformEx();
93-
AdjformEx(const Adjform& adjform, const rational_type& value = 1, const Ex& prefactor = Ex());
94-
AdjformEx(const Adjform& adjform, const rational_type& value, Ex::iterator prefactor);
93+
AdjformEx(const Adjform& adjform, const integer_type& value = 1, const Ex& prefactor = Ex());
94+
AdjformEx(const Adjform& adjform, const integer_type& value, Ex::iterator prefactor);
9595
AdjformEx(Ex& tr, Ex::iterator it, IndexMap& index_map, const Kernel& kernel);
9696

9797
// Check if 'other' is a linear multiple of 'this' and return
9898
// the numeric factor if so, otherwise returns 0
99-
rational_type compare(const AdjformEx& other) const;
99+
integer_type compare(const AdjformEx& other) const;
100100

101101
void combine(const AdjformEx& other); // Add all contributions from 'other' into 'this'
102-
void combine(const AdjformEx& other, rational_type factor);
103-
void multiply(const rational_type& k); // Multiply all terms by a constant factor
102+
void combine(const AdjformEx& other, integer_type factor);
103+
void multiply(const integer_type& k); // Multiply all terms by a constant factor
104104

105105
iterator begin();
106106
const_iterator begin() const;
@@ -119,11 +119,11 @@ namespace cadabra {
119119
Ex& get_tensor_ex();
120120

121121
// Get the value of the term, or zero if it doesn't exist
122-
const rational_type& get(const Adjform& adjform) const;
122+
const integer_type& get(const Adjform& adjform) const;
123123
// Sets the given term to value, creating/removing the term if required
124-
void set(const Adjform& adjform, const rational_type& value = 1);
124+
void set(const Adjform& adjform, const integer_type& value = 1);
125125
// Adds value to the given term, creating/removing the term if required
126-
void add(const Adjform& adjform, const rational_type& value = 1);
126+
void add(const Adjform& adjform, const integer_type& value = 1);
127127

128128
// Symmetrize in the given indices
129129
// e.g. if the only term is abcd then
@@ -143,12 +143,12 @@ namespace cadabra {
143143

144144
private:
145145
// Unsafe (but faster) versions of the public functions
146-
void set_(const Adjform& adjform, const rational_type& value = 1);
147-
void add_(const Adjform& adjform, const rational_type& value = 1);
146+
void set_(const Adjform& adjform, const integer_type& value = 1);
147+
void add_(const Adjform& adjform, const integer_type& value = 1);
148148
map_t data;
149149
Ex prefactor;
150150
Ex tensor;
151-
static rational_type zero;
151+
static integer_type zero;
152152
};
153153
}
154154

core/algorithms/meld.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ bool similar_tensor_form(const Kernel& kernel, Ex& tr, Ex::iterator it1, Ex::ite
342342
meld::result_t meld::apply_tableaux(iterator it)
343343
{
344344
using namespace boost::numeric::ublas;
345-
using matrix_type = matrix<AdjformEx::rational_type>;
346-
using vector_type = vector<AdjformEx::rational_type>;
345+
using matrix_type = matrix<mpq_class>;
346+
using vector_type = vector<mpq_class>;
347347

348348
result_t res = result_t::l_no_action;
349349

@@ -368,7 +368,7 @@ meld::result_t meld::apply_tableaux(iterator it)
368368
// ensure that when solving for linear dependence there are as many
369369
// unknowns as equations
370370
matrix_type coeffs;
371-
LinearSolver<AdjformEx::rational_type> solver;
371+
LinearSolver<mpq_class> solver;
372372

373373
// The adjform in position 'i' of 'mapping' represents the term corresponding
374374
// to the 'i'th row of 'coeffs'
@@ -425,7 +425,7 @@ meld::result_t meld::apply_tableaux(iterator it)
425425
// Ensure that the next term is bigger than any other term to begin with
426426
Adjform next_term;
427427
next_term.push_back(std::numeric_limits<Adjform::value_type>::max());
428-
AdjformEx::rational_type sum = 0;
428+
mpq_class sum = 0;
429429
for (size_t i = 0; i < adjforms.size(); ++i) {
430430
if (lhs_its[i] != adjforms[i].end() && lhs_its[i]->first == cur_term) {
431431
sum += x(i) * lhs_its[i]->second;
@@ -437,7 +437,7 @@ meld::result_t meld::apply_tableaux(iterator it)
437437
next_term = lhs_its[i]->first;
438438
}
439439

440-
AdjformEx::rational_type rhs_sum;
440+
mpq_class rhs_sum;
441441
if (rhs_it == cur_adjform.end() || rhs_it->first != cur_term) {
442442
rhs_sum = 0;
443443
}

0 commit comments

Comments
 (0)