Ian Jauslin
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/fields.c')
-rw-r--r--src/fields.c273
1 files changed, 229 insertions, 44 deletions
diff --git a/src/fields.c b/src/fields.c
index bfd1f39..7be84df 100644
--- a/src/fields.c
+++ b/src/fields.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.
@@ -23,6 +23,13 @@ limitations under the License.
#include "polynomial.h"
#include "array.h"
#include "rational.h"
+#include "tree.h"
+
+//---------------------------------------------------------------------
+//
+// Fields_Table
+//
+//---------------------------------------------------------------------
// init and free for Fields_Table
int init_Fields_Table(Fields_Table* fields){
@@ -30,7 +37,7 @@ int init_Fields_Table(Fields_Table* fields){
init_Int_Array(&((*fields).external),FIELDS_SIZE);
init_Int_Array(&((*fields).internal),FIELDS_SIZE);
init_Identities(&((*fields).ids), FIELDS_SIZE);
- init_Symbols(&((*fields).symbols), FIELDS_SIZE);
+ init_Virtual_fields(&((*fields).virtual_fields), FIELDS_SIZE);
init_Int_Array(&((*fields).fermions),FIELDS_SIZE);
init_Int_Array(&((*fields).noncommuting),FIELDS_SIZE);
return(0);
@@ -40,7 +47,7 @@ int free_Fields_Table(Fields_Table fields){
free_Int_Array(fields.external);
free_Int_Array(fields.internal);
free_Identities(fields.ids);
- free_Symbols(fields.symbols);
+ free_Virtual_fields(fields.virtual_fields);
free_Int_Array(fields.fermions);
free_Int_Array(fields.noncommuting);
return(0);
@@ -57,11 +64,11 @@ int field_type(int index, Fields_Table fields){
else if(int_array_find(abs(index), fields.internal)>=0){
return(FIELD_INTERNAL);
}
- else if(intlist_find(fields.symbols.indices, fields.symbols.length, index)>=0){
- return(FIELD_SYMBOL);
+ else if(intlist_find(fields.virtual_fields.indices, fields.virtual_fields.length, index)>=0){
+ return(FIELD_VIRTUAL);
}
- fprintf(stderr,"error: index %d is neither a parameter nor an external or an internal field, nor a symbol\n",index);
+ fprintf(stderr,"error: index %d is neither a parameter nor an external or an internal field, nor a virtual field\n",index);
exit(-1);
}
@@ -86,7 +93,11 @@ int is_noncommuting(int index, Fields_Table fields){
}
-// ------------------ Identities --------------------
+//---------------------------------------------------------------------
+//
+// Identities
+//
+//---------------------------------------------------------------------
// allocate memory
int init_Identities(Identities* identities,int size){
@@ -314,6 +325,11 @@ int int_array_is_subarray_noncommuting(Int_Array input, Int_Array test_array, Fi
int match_nc;
int first=-1;
+ // cannot fit
+ if(test_array.length<input.length){
+ return(-1);
+ }
+
// bound noncommuting elements
while(is_noncommuting(input.values[post_nc], fields)==1){
post_nc++;
@@ -324,7 +340,7 @@ int int_array_is_subarray_noncommuting(Int_Array input, Int_Array test_array, Fi
match_nc=1;
}
for(j=1;j<post_nc;j++){
- if(test_array.values[i+j]!=input.values[j]){
+ if(i+j>=test_array.length || test_array.values[i+j]!=input.values[j]){
match_nc=0;
}
}
@@ -359,57 +375,61 @@ int int_array_is_subarray_noncommuting(Int_Array input, Int_Array test_array, Fi
}
-// ------------------ Symbols --------------------
+//---------------------------------------------------------------------
+//
+// Virtual_fields
+//
+//---------------------------------------------------------------------
// allocate memory
-int init_Symbols(Symbols* symbols,int size){
- (*symbols).indices=calloc(size,sizeof(int));
- (*symbols).expr=calloc(size,sizeof(Polynomial));
- (*symbols).length=0;
- (*symbols).memory=size;
+int init_Virtual_fields(Virtual_fields* virtual_fields,int size){
+ (*virtual_fields).indices=calloc(size,sizeof(int));
+ (*virtual_fields).expr=calloc(size,sizeof(Polynomial));
+ (*virtual_fields).length=0;
+ (*virtual_fields).memory=size;
return(0);
}
// free memory
-int free_Symbols(Symbols symbols){
+int free_Virtual_fields(Virtual_fields virtual_fields){
int i;
- for(i=0;i<symbols.length;i++){
- free_Polynomial(symbols.expr[i]);
+ for(i=0;i<virtual_fields.length;i++){
+ free_Polynomial(virtual_fields.expr[i]);
}
- free(symbols.indices);
- free(symbols.expr);
+ free(virtual_fields.indices);
+ free(virtual_fields.expr);
return(0);
}
// resize
-int resize_symbols(Symbols* symbols,int new_size){
- Symbols new_symbols;
+int resize_virtual_fields(Virtual_fields* virtual_fields,int new_size){
+ Virtual_fields new_virtual_fields;
int i;
- init_Symbols(&new_symbols,new_size);
- for(i=0;i<(*symbols).length;i++){
- new_symbols.indices[i]=(*symbols).indices[i];
- new_symbols.expr[i]=(*symbols).expr[i];
+ init_Virtual_fields(&new_virtual_fields,new_size);
+ for(i=0;i<(*virtual_fields).length;i++){
+ new_virtual_fields.indices[i]=(*virtual_fields).indices[i];
+ new_virtual_fields.expr[i]=(*virtual_fields).expr[i];
}
- new_symbols.length=(*symbols).length;
+ new_virtual_fields.length=(*virtual_fields).length;
- free((*symbols).indices);
- free((*symbols).expr);
+ free((*virtual_fields).indices);
+ free((*virtual_fields).expr);
- *symbols=new_symbols;
+ *virtual_fields=new_virtual_fields;
return(0);
}
// copy
-int symbols_cpy(Symbols input, Symbols* output){
- init_Symbols(output,input.length);
- symbols_cpy_noinit(input,output);
+int virtual_fields_cpy(Virtual_fields input, Virtual_fields* output){
+ init_Virtual_fields(output,input.length);
+ virtual_fields_cpy_noinit(input,output);
return(0);
}
-int symbols_cpy_noinit(Symbols input, Symbols* output){
+int virtual_fields_cpy_noinit(Virtual_fields input, Virtual_fields* output){
int i;
if((*output).memory<input.length){
- fprintf(stderr,"error: trying to copy a symbols collection of length %d to another with memory %d\n",input.length,(*output).memory);
+ fprintf(stderr,"error: trying to copy a virtual fields collection of length %d to another with memory %d\n",input.length,(*output).memory);
exit(-1);
}
for(i=0;i<input.length;i++){
@@ -421,12 +441,12 @@ int symbols_cpy_noinit(Symbols input, Symbols* output){
return(0);
}
-// append an element to a symbols
-int symbols_append(int index, Polynomial expr, Symbols* output){
+// append an element to a virtual_fields
+int virtual_fields_append(int index, Polynomial expr, Virtual_fields* output){
int offset=(*output).length;
if((*output).length>=(*output).memory){
- resize_symbols(output,2*(*output).memory+1);
+ resize_virtual_fields(output,2*(*output).memory+1);
}
// copy and allocate
@@ -436,12 +456,12 @@ int symbols_append(int index, Polynomial expr, Symbols* output){
(*output).length++;
return(0);
}
-// append an element to a symbols without allocating memory
-int symbols_append_noinit(int index, Polynomial expr, Symbols* output){
+// append an element to a virtual_fields without allocating memory
+int virtual_fields_append_noinit(int index, Polynomial expr, Virtual_fields* output){
int offset=(*output).length;
if((*output).length>=(*output).memory){
- resize_symbols(output,2*(*output).memory+1);
+ resize_virtual_fields(output,2*(*output).memory+1);
}
// copy without allocating
@@ -452,18 +472,22 @@ int symbols_append_noinit(int index, Polynomial expr, Symbols* output){
return(0);
}
-// concatenate two symbolss
-int symbols_concat(Symbols input, Symbols* output){
+// concatenate two virtual_fields
+int virtual_fields_concat(Virtual_fields input, Virtual_fields* output){
int i;
for(i=0;i<input.length;i++){
- symbols_append(input.indices[i],input.expr[i],output);
+ virtual_fields_append(input.indices[i],input.expr[i],output);
}
return(0);
}
-// ------------------ Groups --------------------
+//---------------------------------------------------------------------
+//
+// Groups
+//
+//---------------------------------------------------------------------
// allocate memory
int init_Groups(Groups* groups,int size){
@@ -570,3 +594,164 @@ int find_group(int index, Groups groups){
}
return(-1);
}
+
+
+//---------------------------------------------------------------------
+//
+// Variables
+//
+//---------------------------------------------------------------------
+
+// allocate memory
+int init_Variables(Variables* variables,int size){
+ (*variables).var_names=calloc(size,sizeof(Char_Array));
+ (*variables).symbol_trees=calloc(size,sizeof(Tree));
+ (*variables).length=0;
+ (*variables).memory=size;
+ return(0);
+}
+
+// free memory
+int free_Variables(Variables variables){
+ int i;
+ for(i=0;i<variables.length;i++){
+ free_Char_Array(variables.var_names[i]);
+ free_Tree(variables.symbol_trees[i]);
+ }
+ free(variables.var_names);
+ free(variables.symbol_trees);
+ return(0);
+}
+
+// resize
+int resize_variables(Variables* variables,int new_size){
+ Variables new_variables;
+ int i;
+
+ init_Variables(&new_variables,new_size);
+ for(i=0;i<(*variables).length;i++){
+ new_variables.var_names[i]=(*variables).var_names[i];
+ new_variables.symbol_trees[i]=(*variables).symbol_trees[i];
+ }
+ new_variables.length=(*variables).length;
+
+ free((*variables).var_names);
+ free((*variables).symbol_trees);
+
+ *variables=new_variables;
+ return(0);
+}
+
+// copy
+int variables_cpy(Variables input, Variables* output){
+ init_Variables(output,input.length);
+ variables_cpy_noinit(input,output);
+ return(0);
+}
+int variables_cpy_noinit(Variables input, Variables* output){
+ int i;
+ if((*output).memory<input.length){
+ fprintf(stderr,"error: trying to copy a variables collection of length %d to another with memory %d\n",input.length,(*output).memory);
+ exit(-1);
+ }
+ for(i=0;i<input.length;i++){
+ char_array_cpy(input.var_names[i], (*output).var_names+i);
+ tree_cpy(input.symbol_trees[i],(*output).symbol_trees+i);
+ }
+ (*output).length=input.length;
+
+ return(0);
+}
+
+// append an element to a variables collection
+int variables_append(Char_Array var_name, Tree symbol_tree, Variables* output){
+ int offset=(*output).length;
+
+ if((*output).length>=(*output).memory){
+ resize_variables(output,2*(*output).memory+1);
+ }
+
+ // copy and allocate
+ char_array_cpy(var_name,(*output).var_names+offset);
+ tree_cpy(symbol_tree,(*output).symbol_trees+offset);
+ // increment length
+ (*output).length++;
+ return(0);
+}
+// append an element to a variables collection without allocating memory
+int variables_append_noinit(Char_Array var_name, Tree symbol_tree, Variables* output){
+ int offset=(*output).length;
+
+ if((*output).length>=(*output).memory){
+ resize_variables(output,2*(*output).memory+1);
+ }
+
+ // copy without allocating
+ (*output).var_names[offset]=var_name;
+ (*output).symbol_trees[offset]=symbol_tree;
+ // increment length
+ (*output).length++;
+ return(0);
+}
+
+// concatenate two variables collections
+int variables_concat(Variables input, Variables* output){
+ int i;
+ for(i=0;i<input.length;i++){
+ variables_append(input.var_names[i], input.symbol_trees[i], output);
+ }
+ return(0);
+}
+
+
+// find a variable matching a var_name
+int variables_find_var(Char_Array name, Variables variables, Tree* output){
+ Char_Array varname;
+ int i;
+
+ // drop inital '$'
+ char_array_substring(name, 1, name.length-1, &varname);
+ for(i=0;i<variables.length;i++){
+ if(char_array_cmp(varname, variables.var_names[i])==1){
+ tree_cpy(variables.symbol_trees[i], output);
+ break;
+ }
+ }
+
+ // error if no variable was found
+ if(i==variables.length){
+ fprintf(stderr, "error: variable '$%s' not found\n",char_array_to_str_noinit(&varname));
+ exit(-1);
+ }
+ free_Char_Array(varname);
+
+ return(0);
+}
+
+
+// add a polynomials as a new named variable
+int add_polynomial_to_variables(char* name, Polynomial polynomial, Variables* variables){
+ // save polynomial to string (to convert it to a variable, it must first be a string)
+ Char_Array poly_str;
+ Char_Array out_name;
+ Tree out_tree;
+
+ init_Char_Array(&poly_str, STR_SIZE);
+ polynomial_sprint(polynomial, &poly_str);
+
+ // convert name to Char_Array
+ init_Char_Array(&out_name,STR_SIZE);
+ char_array_append_str(name, &out_name);
+
+ // trivial tree containing the polynomial
+ init_Tree(&out_tree,0,poly_str.length);
+ tree_set_label(poly_str, &out_tree);
+ free_Char_Array(poly_str);
+
+ // add variable
+ variables_append(out_name, out_tree, variables);
+ free_Tree(out_tree);
+ free_Char_Array(out_name);
+
+ return 0;
+}