1 // Copyright (c) 1997-2006 John Abbott
2
3 // This file is part of the source of CoCoALib, the CoCoA Library.
4
5 // CoCoALib is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9
10 // CoCoALib is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14
15 // You should have received a copy of the GNU General Public License
16 // along with CoCoALib. If not, see <http://www.gnu.org/licenses/>.
17
18 #include "DMPZfactors.h"
19 #include "jalloc.h"
20
21
DMPZfactors_ctor()22 DMPZfactors DMPZfactors_ctor()
23 {
24 DMPZfactors THIS;
25
26 THIS = (DMPZfactors)MALLOC(sizeof(struct DMPZfactors_struct));
27 THIS->list = NULL;
28 THIS->multiplicity = 1;
29 mpz_init_set_ui(THIS->content, 1);
30 return THIS;
31 }
32
33
DMPZfactors_dtor(DMPZfactors THIS)34 void DMPZfactors_dtor(DMPZfactors THIS)
35 {
36 mpz_clear(THIS->content);
37 DMPZlist_dtor(THIS->list);
38 FREE(THIS);
39 }
40
DMPZfactors_content(DMPZfactors THIS,mpz_t n)41 void DMPZfactors_content(DMPZfactors THIS, mpz_t n)
42 {
43 mpz_mul(THIS->content, THIS->content, n);
44 }
45
46
DMPZfactors_negate(DMPZfactors THIS)47 void DMPZfactors_negate(DMPZfactors THIS)
48 {
49 if (THIS->multiplicity & 1) mpz_neg(THIS->content, THIS->content);
50 }
51
52
DMPZfactors_add(DMPZfactors THIS,const DMPZ f)53 void DMPZfactors_add(DMPZfactors THIS, const DMPZ f)
54 {
55 DMPZ fcopy;
56
57 fcopy = DMPZcopy(f);
58 /* force LC (wrt to deglex) > 0; order deglex imposed by DMPZfactor(..) */
59 fcopy = DMPZsort(fcopy, DMPZorder_deglex);
60 if (mpz_sgn(fcopy->coeff) < 0) DMPZnegate(fcopy);
61 THIS->list = DMPZlist_append(THIS->list,
62 DMPZlist_ctor(fcopy, THIS->multiplicity));
63 }
64
DMPZfactors_multiplicity(DMPZfactors THIS,int m)65 void DMPZfactors_multiplicity(DMPZfactors THIS, int m)
66 {
67 THIS->multiplicity = m;
68 }
69
DMPZfactors_nfactors(const DMPZfactors THIS)70 int DMPZfactors_nfactors(const DMPZfactors THIS)
71 {
72 DMPZlist iter;
73 int nfactors;
74
75 nfactors = 0;
76 for (iter=THIS->list; iter; iter = iter->next)
77 nfactors += iter->deg;
78 return nfactors;
79 }
80