43#include "Teuchos_Assert.hpp"
45template <
typename ordinal_type,
typename value_type>
52template <
typename ordinal_type,
typename value_type>
55add_term(ordinal_type i, ordinal_type
j, ordinal_type k,
const value_type& c)
58 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
true, std::logic_error,
59 "You can't call add_term() after calling fillComplete()!");
62 kji_data[k][
j][i] = c;
63 ikj_data[i][k][
j] = c;
66template <
typename ordinal_type,
typename value_type>
69sum_term(ordinal_type i, ordinal_type
j, ordinal_type k,
const value_type& c)
72 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
true, std::logic_error,
73 "You can't call sum_term() after calling fillComplete()!");
76 kji_data[k][
j][i] += c;
77 ikj_data[i][k][
j] += c;
80template <
typename ordinal_type,
typename value_type>
88 ikj_array.resize(ikj_data.size());
90 for (
typename ikj_map::const_iterator i_it = ikj_data.begin();
91 i_it != ikj_data.end(); ++i_it) {
92 ikj_array.indices[n] = i_it->first;
93 ikj_array.values[n].resize(i_it->second.size());
95 for (
typename kj_map::const_iterator k_it = i_it->second.begin();
96 k_it != i_it->second.end(); ++k_it) {
97 ikj_array.values[n].indices[l] = k_it->first;
98 ikj_array.values[n].values[l].resize(k_it->second.size());
100 for (
typename j_map::const_iterator j_it = k_it->second.begin();
101 j_it != k_it->second.end(); ++j_it) {
102 ikj_array.values[n].values[l].indices[m] = j_it->first;
103 ikj_array.values[n].values[l].values[m] = j_it->second;
111 kji_array.resize(kji_data.size());
113 for (
typename kji_map::const_iterator k_it = kji_data.begin();
114 k_it != kji_data.end(); ++k_it) {
115 kji_array.indices[n] = k_it->first;
116 kji_array.values[n].resize(k_it->second.size());
118 for (
typename ji_map::const_iterator j_it = k_it->second.begin();
119 j_it != k_it->second.end(); ++j_it) {
120 kji_array.values[n].indices[l] = j_it->first;
121 kji_array.values[n].values[l].resize(j_it->second.size());
123 for (
typename i_map::const_iterator i_it = j_it->second.begin();
124 i_it != j_it->second.end(); ++i_it) {
125 kji_array.values[n].values[l].indices[m] = i_it->first;
126 kji_array.values[n].values[l].values[m] = i_it->second;
137 fill_completed =
true;
140template <
typename ordinal_type,
typename value_type>
145 return fill_completed;
148template <
typename ordinal_type,
typename value_type>
151print(std::ostream& os)
const
154 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
155 "You must call fillComplete() before calling print()!");
161 os <<
"k = " << index(k)
162 <<
", j = " << index(
j)
163 <<
", i = " << index(i)
164 <<
", Cijk = " << value(i) << std::endl;
167template <
typename ordinal_type,
typename value_type>
170getValue(ordinal_type i, ordinal_type
j, ordinal_type k)
const
173 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
174 "You must call fillComplete() before calling getValue()!");
179 return value_type(0);
182 if (j_it == j_end(k_it))
183 return value_type(0);
186 if (i_it == i_end(j_it))
187 return value_type(0);
192template <
typename ordinal_type,
typename value_type>
198 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
199 "You must call fillComplete() before calling num_entries()!");
202 ordinal_type num = 0;
203 for (
k_iterator k = k_begin(); k != k_end(); ++k)
211template <
typename ordinal_type,
typename value_type>
217 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
218 "You must call fillComplete() before calling num_k()!");
220 return kji_array.size();
223template <
typename ordinal_type,
typename value_type>
229 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
230 "You must call fillComplete() before calling num_j()!");
233 return k.
value().size();
236template <
typename ordinal_type,
typename value_type>
242 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
243 "You must call fillComplete() before calling num_i()!");
245 return j.value().size();
248template <
typename ordinal_type,
typename value_type>
251find_k(ordinal_type k)
const
254 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
255 "You must call fillComplete() before calling find_k()!");
257 return kji_array.find(k);
260template <
typename ordinal_type,
typename value_type>
266 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
267 "You must call fillComplete() before calling find_j()!");
269 return k.value().find(
j);
272template <
typename ordinal_type,
typename value_type>
278 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
279 "You must call fillComplete() before calling find_i()!");
281 return j.value().find(i);
284template <
typename ordinal_type,
typename value_type>
290 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
291 "You must call fillComplete() before calling k_begin()!");
293 return kji_array.begin();
296template <
typename ordinal_type,
typename value_type>
302 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
303 "You must call fillComplete() before calling k_end()!");
305 return kji_array.end();
308template <
typename ordinal_type,
typename value_type>
314 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
315 "You must call fillComplete() before calling k_rbegin()!");
317 return kji_array.rbegin();
320template <
typename ordinal_type,
typename value_type>
326 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
327 "You must call fillComplete() before calling k_rend()!");
329 return kji_array.rend();
332template <
typename ordinal_type,
typename value_type>
338 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
339 "You must call fillComplete() before calling j_begin()!");
341 return k.value().begin();
344template <
typename ordinal_type,
typename value_type>
350 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
351 "You must call fillComplete() before calling j_end()!");
353 return k.value().end();
356template <
typename ordinal_type,
typename value_type>
362 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
363 "You must call fillComplete() before calling j_begin()!");
365 return k.value().begin();
368template <
typename ordinal_type,
typename value_type>
374 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
375 "You must call fillComplete() before calling j_end()!");
377 return k.value().end();
380template <
typename ordinal_type,
typename value_type>
386 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
387 "You must call fillComplete() before calling i_begin()!");
389 return j.value().begin();
392template <
typename ordinal_type,
typename value_type>
398 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
399 "You must call fillComplete() before calling i_end()!");
401 return j.value().end();
404template <
typename ordinal_type,
typename value_type>
410 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
411 "You must call fillComplete() before calling num_i()!");
413 return ikj_array.size();
416template <
typename ordinal_type,
typename value_type>
422 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
423 "You must call fillComplete() before calling num_k()!");
425 return i.value().size();
428template <
typename ordinal_type,
typename value_type>
434 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
435 "You must call fillComplete() before calling num_j()!");
437 return k.
value().size();
440template <
typename ordinal_type,
typename value_type>
443find_i(ordinal_type i)
const
446 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
447 "You must call fillComplete() before calling find_i()!");
449 return ikj_array.find(i);
452template <
typename ordinal_type,
typename value_type>
458 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
459 "You must call fillComplete() before calling find_k()!");
461 return i.value().find(k);
464template <
typename ordinal_type,
typename value_type>
470 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
471 "You must call fillComplete() before calling find_j()!");
476template <
typename ordinal_type,
typename value_type>
482 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
483 "You must call fillComplete() before calling i_begin()!");
485 return ikj_array.begin();
488template <
typename ordinal_type,
typename value_type>
494 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
495 "You must call fillComplete() before calling i_end()!");
497 return ikj_array.end();
500template <
typename ordinal_type,
typename value_type>
506 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
507 "You must call fillComplete() before calling i_rbegin()!");
509 return ikj_array.rbegin();
512template <
typename ordinal_type,
typename value_type>
518 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
519 "You must call fillComplete() before calling i_rend()!");
521 return ikj_array.rend();
524template <
typename ordinal_type,
typename value_type>
530 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
531 "You must call fillComplete() before calling k_begin()!");
533 return i.value().begin();
536template <
typename ordinal_type,
typename value_type>
542 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
543 "You must call fillComplete() before calling k_end()!");
545 return i.value().end();
548template <
typename ordinal_type,
typename value_type>
554 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
555 "You must call fillComplete() before calling k_begin()!");
557 return i.value().begin();
560template <
typename ordinal_type,
typename value_type>
566 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
567 "You must call fillComplete() before calling k_end()!");
569 return i.value().end();
572template <
typename ordinal_type,
typename value_type>
578 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
579 "You must call fillComplete() before calling j_begin()!");
581 return k.
value().begin();
584template <
typename ordinal_type,
typename value_type>
590 TEUCHOS_TEST_FOR_EXCEPTION(fill_completed ==
false, std::logic_error,
591 "You must call fillComplete() before calling j_end()!");
593 return k.
value().end();
ordinal_type num_j(const k_iterator &k) const
Number of j entries in C(i,j,k) for given k.
i_iterator i_end() const
Iterator pointing to last k entry.
void sum_term(ordinal_type i, ordinal_type j, ordinal_type k, const value_type &c)
Add new term for given (i,j,k) and sum in if already there.
ikj_sparse_array::const_reverse_iterator i_reverse_iterator
Iterator for looping over i entries in reverse.
k_reverse_iterator k_rbegin() const
Reverse iterator pointing to last k entry.
i_reverse_iterator i_rbegin() const
Reverse iterator pointing to last k entry.
k_reverse_iterator k_rend() const
Reverse iterator pointing to first k entry.
kj_iterator j_end(const k_iterator &k) const
Iterator pointing to last j entry for given k.
void print(std::ostream &os) const
Print tensor.
ordinal_type num_i() const
Number of i entries in C(i,j,k)
k_iterator k_begin() const
Iterator pointing to first k entry.
kji_sparse_array::const_iterator k_iterator
Iterator for looping over k entries.
value_type getValue(ordinal_type i, ordinal_type j, ordinal_type k) const
Get Cijk value for a given i, j, k indices.
i_reverse_iterator i_rend() const
Reverse iterator pointing to first k entry.
ikj_sparse_array::const_iterator i_iterator
Iterator for looping over i entries.
void fillComplete()
Signal all terms have been added.
Sparse3Tensor()
Constructor.
kj_iterator j_begin(const k_iterator &k) const
Iterator pointing to first j entry for given k.
k_iterator find_k(ordinal_type k) const
Return k iterator for given index k.
ordinal_type num_k() const
Number of k entries in C(i,j,k)
i_iterator i_begin() const
Iterator pointing to first k entry.
kj_iterator find_j(const k_iterator &k, ordinal_type j) const
Return j iterator given k iterator and index j.
kji_sparse_array::const_reverse_iterator k_reverse_iterator
Iterator for looping over k entries in reverse.
ordinal_type num_entries() const
Return number of non-zero entries.
kji_iterator find_i(const kj_iterator &j, ordinal_type i) const
Return i iterator given j iterator and index i.
k_iterator k_end() const
Iterator pointing to last k entry.
void add_term(ordinal_type i, ordinal_type j, ordinal_type k, const value_type &c)
Add new term for given (i,j,k)
bool fillCompleted() const
Return whether fillComplete() has been called.
Bi-directional iterator for traversing a sparse array.
value_reference value() const
Return value associated with iterator.