From 7d80c871ff4dd6a5ae04cc40aaed69b12f722ad5 Mon Sep 17 00:00:00 2001 From: Cotton Seed Date: Wed, 29 Feb 2012 22:21:45 -0500 Subject: [PATCH] Added direct_sum module and some simple tests to main. --- algebra/module.h | 179 +++++++++++++++++++++++++++++++++++++++++++---- main.cpp | 4 +- 2 files changed, 168 insertions(+), 15 deletions(-) diff --git a/algebra/module.h b/algebra/module.h index 80aaf59..92d98d8 100644 --- a/algebra/module.h +++ b/algebra/module.h @@ -5,6 +5,7 @@ template class mod_span; template class free_submodule; template class quotient_module; +template class direct_sum; template class tensor_product; template class hom_module; @@ -17,11 +18,8 @@ class module : public refcounted static unsigned id_counter; -#if 0 static map, - ptr > direct_sum_idx; -#endif - + ptr > > direct_sum_idx; static map, ptr > > tensor_product_idx; static map, @@ -78,6 +76,37 @@ class module : public refcounted multivariate_laurentpoly free_poincare_polynomial () const; multivariate_laurentpoly free_delta_poincare_polynomial () const; + ptr > + add (basedvector >, 1> compound_summands) const; + + ptr > add (ptr > m) const + { + basedvector >, 1> summands (2); + summands[1] = this; + summands[2] = m; + return add (summands); + } + + // g -> (g, 0) + unsigned inject_1 (unsigned g, ptr > m) const + { + return g; + } + + // g -> (0, g) + unsigned inject_2 (ptr > m, unsigned g) const + { + return dim () + g; + } + + pair project (ptr > m, unsigned g) const + { + if (g <= dim ()) + return pair (1, g); + else + return pair (2, g - dim ()); + } + ptr > tensor (ptr > m) const { basedvector >, 1> factors (2); @@ -104,6 +133,11 @@ class module : public refcounted return (i - 1) + (j - 1) * dim () + 1; } + virtual void append_direct_summands (basedvector >, 1> &summands) const + { + summands.append (this); + } + virtual void append_tensor_factors (basedvector >, 1> &factors) const { factors.append (this); @@ -114,13 +148,16 @@ class module : public refcounted }; template unsigned module::id_counter = 1; + +template map, + ptr > > module::direct_sum_idx; + template map, ptr > > module::tensor_product_idx; template map, ptr > > module::hom_module_idx; -#if 0 template class direct_sum : public module { @@ -130,25 +167,97 @@ class direct_sum : public module public: direct_sum (basedvector >, 1> summands_) : n(0), - summands(summands_) + summands(summands_) { #ifndef NDEBUG - for (unsigned i = 1; i <= terms.size (); i ++) - assert (terms[i]->is_free ()); + for (unsigned i = 1; i <= summands.size (); i ++) + assert (summands[i]->is_free ()); #endif - for (unsigned i = 1; i <= terms.size (); i ++) - n += terms[i]->dim (); + for (unsigned i = 1; i <= summands.size (); i ++) + n += summands[i]->dim (); } + ~direct_sum () { } unsigned dim () const { return n; } unsigned free_rank () const { return n; } - // ??? - grading generator_grading (unsigned i) const { abort (); } - void show_generator (unsigned i) const { abort (); } + grading generator_grading (unsigned i) const; + void show_generator (unsigned i) const; + R generator_ann (unsigned i) const { return R (0); } + + void append_direct_summands (basedvector >, 1> &psummands) const + { + for (unsigned i = 1; i <= summands.size (); i ++) + psummands.append (summands[i]); + } + + unsigned inject (unsigned i, unsigned g) const; + pair project (unsigned g) const; }; -#endif + +template grading +direct_sum::generator_grading (unsigned i) const +{ + pair p = project (i); + return summands[p.first]->generator_grading (p.second); +} + +template void +direct_sum::show_generator (unsigned i) const +{ + pair p = project (i); + printf ("%d:", p.first); + return summands[p.first]->show_generator (p.second); +} + +template unsigned +direct_sum::inject (unsigned i, unsigned g) const +{ + assert (i >= 1 && i <= summands.size ()); + + for (unsigned j = 1; j < i; j ++) + g += summands[j]->dim (); + return g; +} + +template pair +direct_sum::project (unsigned g) const +{ + assert (g <= n); + + unsigned g0 = g; + + for (unsigned j = 1; j <= summands.size (); j ++) + { + if (g <= summands[j]->dim ()) + { + assert (inject (j, g) == g0); + + return pair (j, g); + } + else + g -= summands[j]->dim (); + } + abort (); // shouldn't get here +} + +template ptr > +module::add (basedvector >, 1> compound_summands) const +{ + basedvector >, 1> summands; + for (unsigned i = 1; i <= compound_summands.size (); i ++) + compound_summands[i]->append_direct_summands (summands); + + basedvector summand_ids (summands.size ()); + for (unsigned i = 1; i <= summands.size (); i ++) + summand_ids[i] = summands[i]->id; + + pair > &, bool> p = direct_sum_idx.find (summand_ids); + if (!p.second) + p.first = new direct_sum (summands); + return p.first; +} template class tensor_product : public module @@ -597,6 +706,41 @@ class composition_impl : public map_impl } }; +template +class direct_sum_impl : public map_impl +{ + // f\oplus g + ptr > f, g; + + public: + direct_sum_impl (ptr > f_, ptr > g_) + : map_impl(f_->from->add (g_->from), + f_->to->add (g_->to)), + f(f_), + g(g_) + { + } + + linear_combination column (unsigned i) const + { + pair p = f->from->project (g->from, i); + + linear_combination r (this->to); + if (p.first == 1) + { + for (linear_combination_const_iter j = f->column (p.second); j; j ++) + r.muladd (j.val (), f->to->inject_1 (j.key (), g->to)); + } + else + { + assert (p.first == 2); + for (linear_combination_const_iter j = g->column (p.second); j; j ++) + r.muladd (j.val (), f->to->inject_2 (g->to, j.key ())); + } + return r; + } +}; + template class tensor_impl : public map_impl { @@ -749,6 +893,13 @@ class mod_map new composition_impl (impl, m.impl)); } + // ??? in the sense of direct sum + mod_map add (const mod_map &m) const + { + return mod_map (IMPL, + new direct_sum_impl (impl, m.impl)); + } + mod_map tensor (const mod_map &m) const { return mod_map (IMPL, diff --git a/main.cpp b/main.cpp index 2df7032..b6f8986 100644 --- a/main.cpp +++ b/main.cpp @@ -193,7 +193,9 @@ main () gb[3].muladd (-6, 2); mod_map g (gb); display ("g:\n", g); - + + display ("f oplus g:\n", f.add (g)); + map_builder hb (E, F); hb[1].muladd (3, 2); hb[2].muladd (-3, 1);