Ian Jauslin
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/polynomial.c')
-rw-r--r--src/polynomial.c77
1 files changed, 69 insertions, 8 deletions
diff --git a/src/polynomial.c b/src/polynomial.c
index d082b60..fae0c08 100644
--- a/src/polynomial.c
+++ b/src/polynomial.c
@@ -1,5 +1,5 @@
/*
-Copyright 2015 Ian Jauslin
+Copyright 2015-2022 Ian Jauslin
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@ limitations under the License.
#include "array.h"
#include "number.h"
#include "fields.h"
+#include "parse_file.h"
// allocate memory
@@ -368,7 +369,7 @@ int polynomial_prod_chain_nosimplify(Polynomial input, Polynomial* inout, Fields
Int_Array out_monomial;
Int_Array out_factor;
Number out_num;
- // save length of inout (which changes during the loop
+ // save length of inout (which changes during the loop)
int inout_length=(*inout).length;
// first position in input which can multiply a term of inout without vanishing
int firstpos;
@@ -630,8 +631,8 @@ int remove_unmatched_plusminus(Polynomial* polynomial, Fields_Table fields){
match_internals--;
}
}
- // don't remove a term containing symbols
- else if(type==FIELD_SYMBOL){
+ // don't remove a term containing virtual_field
+ else if(type==FIELD_VIRTUAL){
match_internals=0;
break;
}
@@ -1093,7 +1094,7 @@ int polynomial_print(Polynomial polynomial){
Char_Array buffer;
init_Char_Array(&buffer, STR_SIZE);
polynomial_sprint(polynomial, &buffer);
- printf("%s",buffer.str);
+ printf("%s",char_array_to_str_noinit(&buffer));
free_Char_Array(buffer);
return(0);
}
@@ -1104,7 +1105,7 @@ int polynomial_print(Polynomial polynomial){
#define PP_MONOMIAL_MODE 2
#define PP_FACTOR_MODE 3
#define PP_NUMBER_MODE 4
-int Char_Array_to_Polynomial(Char_Array str_polynomial,Polynomial* output){
+int Char_Array_to_Polynomial(Char_Array str_polynomial, Polynomial* output){
// buffer
char* buffer=calloc(str_polynomial.length+1,sizeof(char));
char* buffer_ptr=buffer;
@@ -1115,6 +1116,7 @@ int Char_Array_to_Polynomial(Char_Array str_polynomial,Polynomial* output){
int comment=0;
int i,j;
int parenthesis_count=0;
+ int ret;
// allocate memory
init_Polynomial(output,POLY_SIZE);
@@ -1174,6 +1176,10 @@ int Char_Array_to_Polynomial(Char_Array str_polynomial,Polynomial* output){
init_Int_Array(&factor, MONOMIAL_SIZE);
num=number_one();
}
+ else{
+ fprintf(stderr,"syntax error: misplaced '+' in polynomial\n");
+ exit(-1);
+ }
break;
// enter monomial or factor mode
@@ -1181,6 +1187,10 @@ int Char_Array_to_Polynomial(Char_Array str_polynomial,Polynomial* output){
if(mode==PP_NULL_MODE){
mode=PP_BRACKET_MODE;
}
+ else{
+ fprintf(stderr,"syntax error: misplaced '[' in polynomial\n");
+ exit(-1);
+ }
break;
// factor mode
case 'l':
@@ -1189,6 +1199,10 @@ int Char_Array_to_Polynomial(Char_Array str_polynomial,Polynomial* output){
buffer_ptr=buffer;
*buffer_ptr='\0';
}
+ else{
+ fprintf(stderr,"syntax error: misplaced 'l' in polynomial\n");
+ exit(-1);
+ }
break;
// monomial mode
case 'f':
@@ -1197,16 +1211,30 @@ int Char_Array_to_Polynomial(Char_Array str_polynomial,Polynomial* output){
buffer_ptr=buffer;
*buffer_ptr='\0';
}
+ else{
+ fprintf(stderr,"syntax error: misplaced 'j' in polynomial\n");
+ exit(-1);
+ }
break;
// read monomial or factor
case ']':
sscanf(buffer,"%d",&i);
+ ret=read_int(buffer,&i);
+ if(ret<0){
+ fprintf(stderr,"syntax error: in polynomial, expected integer field or factor index, got '%s'\n",buffer);
+ exit(-1);
+ }
+
if(mode==PP_FACTOR_MODE){
int_array_append(i,&factor);
}
else if(mode==PP_MONOMIAL_MODE){
int_array_append(i,&monomial);
}
+ else{
+ fprintf(stderr,"syntax error: mismatched ']' in polynomial\n");
+ exit(-1);
+ }
// switch back to null mode
mode=PP_NULL_MODE;
break;
@@ -1224,6 +1252,10 @@ int Char_Array_to_Polynomial(Char_Array str_polynomial,Polynomial* output){
parenthesis_count++;
buffer_ptr=str_addchar(buffer_ptr,str_polynomial.str[j]);
}
+ else{
+ fprintf(stderr,"syntax error: misplaced '(' in polynomial\n");
+ exit(-1);
+ }
break;
case ')':
if(mode==PP_NUMBER_MODE){
@@ -1240,11 +1272,14 @@ int Char_Array_to_Polynomial(Char_Array str_polynomial,Polynomial* output){
buffer_ptr=str_addchar(buffer_ptr,str_polynomial.str[j]);
}
}
+ else{
+ fprintf(stderr,"syntax error: mismatched ')' in polynomial\n");
+ exit(-1);
+ }
break;
// characters to ignore
case ' ':break;
- case '&':break;
case '\n':break;
// comments
@@ -1257,6 +1292,10 @@ int Char_Array_to_Polynomial(Char_Array str_polynomial,Polynomial* output){
// write to buffer
buffer_ptr=str_addchar(buffer_ptr,str_polynomial.str[j]);
}
+ else{
+ fprintf(stderr,"syntax error: in polynomial, unrecognized character '%c'\n",str_polynomial.str[j]);
+ exit(-1);
+ }
break;
}
}
@@ -1270,7 +1309,7 @@ int Char_Array_to_Polynomial(Char_Array str_polynomial,Polynomial* output){
}
// with str input
-int str_to_Polynomial(char* str_polynomial,Polynomial* output){
+int str_to_Polynomial(char* str_polynomial, Polynomial* output){
Char_Array buffer;
str_to_char_array(str_polynomial, &buffer);
Char_Array_to_Polynomial(buffer, output);
@@ -1278,6 +1317,15 @@ int str_to_Polynomial(char* str_polynomial,Polynomial* output){
return(0);
}
+// check whether the polynomial is a constant
+int polynomial_is_number(Polynomial poly){
+ if(poly.length==0 || (poly.length==1 && poly.monomials[0].length==0 && poly.factors[0].length==0)){
+ return(1);
+ }
+ else{
+ return(0);
+ }
+}
// -------------------- Polynomial_Matrix ---------------------
@@ -1307,3 +1355,16 @@ int free_Polynomial_Matrix(Polynomial_Matrix matrix){
free(matrix.indices);
return(0);
}
+
+// check whether the entries are numbers
+int polynomial_matrix_is_numeric(Polynomial_Matrix matrix){
+ int i,j;
+ for(i=0;i<matrix.length;i++){
+ for(j=0;j<matrix.length;j++){
+ if(polynomial_is_number(matrix.matrix[i][j])==0){
+ return(0);
+ }
+ }
+ }
+ return(1);
+}