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 <stddef.h>
19 #include "DUPZfactors.h"
20 #include "jalloc.h"
21 #include "jaaerror.h"
22
23
DUPZfactors_ctor()24 DUPZfactors DUPZfactors_ctor()
25 {
26 DUPZfactors THIS;
27
28 THIS = (DUPZfactors)MALLOC(sizeof(struct DUPZfactors_struct));
29 THIS->list = NULL;
30 THIS->multiplicity = 1;
31 THIS->reversed = 0;
32 mpz_init_set_ui(THIS->content, 1);
33 return THIS;
34 }
35
36
DUPZfactors_dtor(DUPZfactors THIS)37 void DUPZfactors_dtor(DUPZfactors THIS)
38 {
39 mpz_clear(THIS->content);
40 DUPZlist_dtor(THIS->list);
41 FREE(THIS);
42 }
43
DUPZfactors_content(DUPZfactors THIS,mpz_t n)44 void DUPZfactors_content(DUPZfactors THIS, mpz_t n)
45 {
46 mpz_mul(THIS->content, THIS->content, n);
47 }
48
49
DUPZfactors_negate(DUPZfactors THIS)50 void DUPZfactors_negate(DUPZfactors THIS)
51 {
52 if (THIS->multiplicity & 1) mpz_neg(THIS->content, THIS->content);
53 }
54
55
DUPZfactors_add(DUPZfactors THIS,const DUPZ f)56 void DUPZfactors_add(DUPZfactors THIS, const DUPZ f)
57 {
58 DUPZ fcopy;
59
60 #ifdef FACTOR_DEBUG
61 printf("%d ", DUPZdeg(f));
62 #endif
63 fcopy = DUPZcopy(f);
64 if (THIS->reversed) DUPZreverse(fcopy);
65 /* The next lines just make sure that the lc of each factor is >0 */
66 /* Also negate the "content" if lc < 0 and the multiplicity is odd. */
67 if (mpz_sgn(DUPZlc(fcopy)) < 0)
68 {
69 DUPZneg1(fcopy);
70 if (THIS->multiplicity&1) mpz_neg(THIS->content, THIS->content);
71 }
72 THIS->list = DUPZlist_append(THIS->list,
73 DUPZlist_ctor(fcopy, THIS->multiplicity));
74 }
75
DUPZfactors_multiplicity(DUPZfactors THIS,int m)76 void DUPZfactors_multiplicity(DUPZfactors THIS, int m)
77 {
78 THIS->multiplicity = m;
79 }
80
DUPZfactors_reversed(DUPZfactors THIS,int yes_no)81 void DUPZfactors_reversed(DUPZfactors THIS, int yes_no)
82 {
83 THIS->reversed = yes_no;
84 }
85
DUPZfactors_nfactors(const DUPZfactors THIS)86 int DUPZfactors_nfactors(const DUPZfactors THIS)
87 {
88 DUPZlist iter;
89 int nfactors;
90
91 nfactors = 0;
92 for (iter=THIS->list; iter; iter = iter->next)
93 nfactors += iter->deg;
94 return nfactors;
95 }
96