My Project
Loading...
Searching...
No Matches
Functions | Variables
interval.cc File Reference
#include "kernel/mod2.h"
#include "Singular/blackbox.h"
#include "Singular/dyn_modules/interval/interval.h"
#include "Singular/ipshell.h"
#include "Singular/links/ssiLink.h"
#include "Singular/mod_lib.h"

Go to the source code of this file.

Functions

static void * interval_Init (blackbox *)
 
static char * interval_String (blackbox *, void *d)
 
static void * interval_Copy (blackbox *, void *d)
 
static void interval_Destroy (blackbox *, void *d)
 
static BOOLEAN interval_Assign (leftv result, leftv args)
 
static BOOLEAN length (leftv result, leftv arg)
 
static intervalintervalScalarMultiply (number a, interval *I)
 
static intervalintervalMultiply (interval *I, interval *J)
 
static intervalintervalAdd (interval *I, interval *J)
 
static intervalintervalSubtract (interval *I, interval *J)
 
static bool intervalEqual (interval *I, interval *J)
 
static bool intervalContainsZero (interval *I)
 
static intervalintervalPower (interval *I, int p)
 
static BOOLEAN interval_Op2 (int op, leftv result, leftv i1, leftv i2)
 
static BOOLEAN interval_serialize (blackbox *, void *d, si_link f)
 
static BOOLEAN interval_deserialize (blackbox **, void **d, si_link f)
 
static void * box_Init (blackbox *)
 
static void * box_Copy (blackbox *, void *d)
 
static void box_Destroy (blackbox *, void *d)
 
static char * box_String (blackbox *, void *d)
 
static BOOLEAN box_Assign (leftv result, leftv args)
 
static BOOLEAN box_Op2 (int op, leftv result, leftv b1, leftv b2)
 
static BOOLEAN box_OpM (int op, leftv result, leftv args)
 
static BOOLEAN box_serialize (blackbox *, void *d, si_link f)
 
static BOOLEAN box_deserialize (blackbox **, void **d, si_link f)
 
static BOOLEAN boxSet (leftv result, leftv args)
 
static BOOLEAN evalPolyAtBox (leftv result, leftv args)
 
int SI_MOD_INIT() interval (SModulFunctions *psModulFunctions)
 

Variables

STATIC_VAR int intervalID
 
STATIC_VAR int boxID
 

Function Documentation

◆ box_Assign()

static BOOLEAN box_Assign ( leftv  result,
leftv  args 
)
static

Definition at line 815 of file interval.cc.

816{
817 box *RES;
818
819 /*
820 * Allow assignments of the form
821 *
822 * B = C,
823 * B = l,
824 *
825 * where B, C boxes, l list of intervals
826 */
827
828 if (args->Typ() == boxID)
829 {
830 box *B = (box*) args->Data();
831 RES = new box(B);
832 }
833 else if (args->Typ() == LIST_CMD)
834 {
835 RES = new box();
836 lists l = (lists) args->Data();
837
838 int i, m = lSize(l), n = currRing->N;
839 // minimum
840 int M = m > (n-1) ? (n-1) : m;
841
842 for (i = 0; i <= M; i++)
843 {
844 if (l->m[i].Typ() != intervalID)
845 {
846 WerrorS("list contains non-intervals");
847 delete RES;
848 args->CleanUp();
849 return TRUE;
850 }
851 RES->setInterval(i, (interval*) l->m[i].CopyD());
852
853 // make sure rings of boxes and their intervals are consistent
854 // this is important for serialization
855 RES->intervals[i]->setRing(RES->R);
856 }
857 }
858 else
859 {
860 WerrorS("Input not supported: first argument not box, list, or interval");
861 return TRUE;
862 }
863
864 // destroy data of result if it exists
865 if (result != NULL && result->Data() != NULL)
866 {
867 delete (box*) result->Data();
868 }
869
870 if (result->rtyp == IDHDL)
871 {
872 IDDATA((idhdl)result->data) = (char*) RES;
873 }
874 else
875 {
876 result->rtyp = boxID;
877 result->data = (void*) RES;
878 }
879 args->CleanUp();
880
881 return FALSE;
882}
#define TRUE
Definition: auxiliary.h:100
#define FALSE
Definition: auxiliary.h:96
int l
Definition: cfEzgcd.cc:100
int m
Definition: cfEzgcd.cc:128
int i
Definition: cfEzgcd.cc:132
Definition: idrec.h:35
int Typ()
Definition: subexpr.cc:1019
void * Data()
Definition: subexpr.cc:1162
void CleanUp(ring r=currRing)
Definition: subexpr.cc:348
Definition: lists.h:24
return result
Definition: facAbsBiFact.cc:75
b *CanonicalForm B
Definition: facBivar.cc:52
void WerrorS(const char *s)
Definition: feFopen.cc:24
STATIC_VAR int boxID
Definition: interval.cc:131
STATIC_VAR int intervalID
Definition: interval.cc:130
#define IDDATA(a)
Definition: ipid.h:126
int lSize(lists L)
Definition: lists.cc:25
slists * lists
Definition: mpr_numeric.h:146
#define NULL
Definition: omList.c:12
VAR ring currRing
Widely used global variable which specifies the current polynomial ring for Singular interpreter and ...
Definition: polys.cc:13
#define M
Definition: sirandom.c:25
Definition: interval.h:22
ring R
Definition: interval.h:24
interval ** intervals
Definition: interval.h:23
box & setInterval(int, interval *)
Definition: interval.cc:116
interval & setRing(ring)
Definition: interval.cc:50
#define IDHDL
Definition: tok.h:31
@ LIST_CMD
Definition: tok.h:118

◆ box_Copy()

static void * box_Copy ( blackbox *  ,
void *  d 
)
static

Definition at line 781 of file interval.cc.

782{
783 return (void*) new box((box*) d);
784}

◆ box_deserialize()

static BOOLEAN box_deserialize ( blackbox **  ,
void **  d,
si_link  f 
)
static

Definition at line 1096 of file interval.cc.

1097{
1098 leftv l;
1099
1100 // read once to set ring
1101 l = f->m->Read(f);
1102
1103 ring R = currRing;
1104 int i, N = R->N;
1105 box *B = new box();
1106
1107 B->setInterval(0, (interval*) l->CopyD());
1108 l->CleanUp();
1109
1110 for (i = 1; i < N; i++)
1111 {
1112 l = f->m->Read(f);
1113 B->setInterval(i, (interval*) l->CopyD());
1114 l->CleanUp();
1115 }
1116
1117 *d = (void*) B;
1118 return FALSE;
1119}
const CanonicalForm CFMap CFMap & N
Definition: cfEzgcd.cc:56
FILE * f
Definition: checklibs.c:9
Class used for (list of) interpreter objects.
Definition: subexpr.h:83
#define R
Definition: sirandom.c:27

◆ box_Destroy()

static void box_Destroy ( blackbox *  ,
void *  d 
)
static

Definition at line 786 of file interval.cc.

787{
788 if (d != NULL)
789 delete (box*) d;
790}

◆ box_Init()

static void * box_Init ( blackbox *  )
static

Definition at line 776 of file interval.cc.

777{
778 return (void*) new box();
779}

◆ box_Op2()

static BOOLEAN box_Op2 ( int  op,
leftv  result,
leftv  b1,
leftv  b2 
)
static

Definition at line 884 of file interval.cc.

885{
886 if (b1 == NULL || b1->Typ() != boxID)
887 {
888 Werror("first argument is not box but type(%d), second is type(%d)",
889 b1->Typ(), b2->Typ());
890 return TRUE;
891 }
892
893 box *B1 = (box*) b1->Data();
894 int n = B1->R->N;
895
896 box *RES;
897 switch(op)
898 {
899 case '[':
900 {
901 if (b2 == NULL || b2->Typ() != INT_CMD)
902 {
903 WerrorS("second argument not int");
904 return TRUE;
905 }
906 if (result->Data() != NULL)
907 {
908 delete (interval*) result->Data();
909 }
910
911 int i = (int)(long) b2->Data();
912
913 if (i < 1 || i > n)
914 {
915 WerrorS("index out of bounds");
916 return TRUE;
917 }
918
919 // delete data of result
920 if (result->Data() != NULL)
921 {
922 delete (interval*) result->Data();
923 }
924
925 result->rtyp = intervalID;
926 result->data = (void*) new interval(B1->intervals[i-1]);
927 b1->CleanUp();
928 b2->CleanUp();
929 return FALSE;
930 }
931 case '-':
932 {
933 if (b2 == NULL || b2->Typ() != boxID)
934 {
935 WerrorS("second argument not box");
936 return TRUE;
937 }
938
939 box *B2 = (box*) b2->Data();
940 // maybe try to skip this initialisation
941 // copying def of box() results in segfault?
942 if (B1->R != B2->R)
943 {
944 WerrorS("subtracting boxes from different rings not supported");
945 return TRUE;
946 }
947 RES = new box();
948 int i;
949 for (i = 0; i < n; i++)
950 {
952 }
953
954 if (result->Data() != NULL)
955 {
956 delete (box*) result->Data();
957 }
958
959 result->rtyp = boxID;
960 result->data = (void*) RES;
961 b1->CleanUp();
962 b2->CleanUp();
963 return FALSE;
964 }
965 case EQUAL_EQUAL:
966 {
967 if (b2 == NULL || b2->Typ() != boxID)
968 {
969 WerrorS("second argument not box");
970 }
971 box *B2 = (box*) b2->Data();
972 int i;
973 bool res = true;
974 for (i = 0; i < n; i++)
975 {
976 if (!intervalEqual(B1->intervals[i], B2->intervals[i]))
977 {
978 res = false;
979 break;
980 }
981 }
982
983 result->rtyp = INT_CMD;
984 result->data = (void*) res;
985 b1->CleanUp();
986 b2->CleanUp();
987 return FALSE;
988 }
989 default:
990 return blackboxDefaultOp2(op, result, b1, b2);
991 }
992}
BOOLEAN blackboxDefaultOp2(int, leftv, leftv, leftv)
default procedure blackboxDefaultOp2, to be called as "default:" branch
Definition: blackbox.cc:97
CanonicalForm res
Definition: facAbsFact.cc:60
@ EQUAL_EQUAL
Definition: grammar.cc:268
static interval * intervalSubtract(interval *I, interval *J)
Definition: interval.cc:343
static bool intervalEqual(interval *I, interval *J)
Definition: interval.cc:354
void Werror(const char *fmt,...)
Definition: reporter.cc:189
@ INT_CMD
Definition: tok.h:96

◆ box_OpM()

static BOOLEAN box_OpM ( int  op,
leftv  result,
leftv  args 
)
static

Definition at line 994 of file interval.cc.

995{
996 leftv a = args;
997 switch(op)
998 {
999 case INTERSECT_CMD:
1000 {
1001 if (args->Typ() != boxID)
1002 {
1003 WerrorS("can only intersect boxes");
1004 return TRUE;
1005 }
1006 box *B = (box*) args->Data();
1007 int i, n = B->R->N;
1008 number lowerb[n], upperb[n];
1009
1010 // do not copy, use same pointers, copy at the end
1011 for (i = 0; i < n; i++)
1012 {
1013 lowerb[i] = B->intervals[i]->lower;
1014 upperb[i] = B->intervals[i]->upper;
1015 }
1016
1017 args = args->next;
1018 while(args != NULL)
1019 {
1020 if (args->Typ() != boxID)
1021 {
1022 WerrorS("can only intersect boxes");
1023 return TRUE;
1024 }
1025
1026 B = (box*) args->Data();
1027 for (i = 0; i < n; i++)
1028 {
1029 if (nGreater(B->intervals[i]->lower, lowerb[i]))
1030 {
1031 lowerb[i] = B->intervals[i]->lower;
1032 }
1033 if (nGreater(upperb[i], B->intervals[i]->upper))
1034 {
1035 upperb[i] = B->intervals[i]->upper;
1036 }
1037
1038 if (nGreater(lowerb[i], upperb[i]))
1039 {
1040 result->rtyp = INT_CMD;
1041 result->data = (void*) (-1);
1042 a->CleanUp();
1043 return FALSE;
1044 }
1045 }
1046 args = args->next;
1047 }
1048
1049 // now copy the numbers
1050 box *RES = new box();
1051 for (i = 0; i < n; i++)
1052 {
1053 RES->setInterval(i, new interval(nCopy(lowerb[i]), nCopy(upperb[i])));
1054 }
1055
1056 result->rtyp = boxID;
1057 result->data = (void*) RES;
1058 a->CleanUp();
1059 return FALSE;
1060 }
1061 default:
1062 return blackboxDefaultOpM(op, result, args);
1063 }
1064}
BOOLEAN blackboxDefaultOpM(int op, leftv res, leftv args)
default procedure blackboxDefaultOpM, to be called as "default:" branch
Definition: blackbox.cc:107
leftv next
Definition: subexpr.h:86
#define nCopy(n)
Definition: numbers.h:15
#define nGreater(a, b)
Definition: numbers.h:28
@ INTERSECT_CMD
Definition: tok.h:100

◆ box_serialize()

static BOOLEAN box_serialize ( blackbox *  ,
void *  d,
si_link  f 
)
static

Definition at line 1066 of file interval.cc.

1067{
1068 /*
1069 * Format: "box" setring intervals[1] .. intervals[N]
1070 */
1071 box *B = (box*) d;
1072 int N = B->R->N, i;
1073 sleftv l, iv;
1074 memset(&l, 0, sizeof(l));
1075 memset(&iv, 0, sizeof(iv));
1076
1077 l.rtyp = STRING_CMD;
1078 l.data = (void*) "box";
1079
1080 f->m->Write(f, &l);
1081 f->m->SetRing(f, B->R, TRUE);
1082
1083 iv.rtyp = intervalID;
1084 for (i = 0; i < N; i++)
1085 {
1086 iv.data = (void*) B->intervals[i];
1087 f->m->Write(f, &iv);
1088 }
1089
1090 if (currRing != B->R)
1091 f->m->SetRing(f, currRing, FALSE);
1092
1093 return FALSE;
1094}
int rtyp
Definition: subexpr.h:91
void * data
Definition: subexpr.h:88
@ STRING_CMD
Definition: tok.h:185

◆ box_String()

static char * box_String ( blackbox *  ,
void *  d 
)
static

Definition at line 792 of file interval.cc.

793{
794 blackbox *b_i = getBlackboxStuff(intervalID);
795 box *B = (box*) d;
796 int i, n = B->R->N;
797
798 if (B == NULL || B->intervals == NULL)
799 {
800 return omStrDup("ooo");
801 }
802
803 StringSetS(interval_String(b_i, (void*) B->intervals[0]));
804
805 for (i = 1; i < n; i++)
806 {
807 // interpret box as Cartesian product, hence use " x "
808 StringAppendS(" x ");
809 StringAppendS(interval_String(b_i, (void*) B->intervals[i]));
810 }
811 return StringEndS();
812}
blackbox * getBlackboxStuff(const int t)
return the structure to the type given by t
Definition: blackbox.cc:17
static char * interval_String(blackbox *, void *d)
Definition: interval.cc:143
#define omStrDup(s)
Definition: omAllocDecl.h:263
void StringSetS(const char *st)
Definition: reporter.cc:128
void StringAppendS(const char *st)
Definition: reporter.cc:107
char * StringEndS()
Definition: reporter.cc:151

◆ boxSet()

static BOOLEAN boxSet ( leftv  result,
leftv  args 
)
static

Definition at line 1121 of file interval.cc.

1122{
1123 // check for proper types
1124 const short t[] = {3, (short) boxID, INT_CMD, (short) intervalID};
1125 if (!iiCheckTypes(args, t, 1))
1126 {
1127 return TRUE;
1128 }
1129 box *B = (box*) args->Data();
1130 int n = B->R->N,
1131 i = (int)(long) args->next->Data();
1132 interval *I = (interval*) args->next->next->Data();
1133
1134 if (i < 1 || i > n)
1135 {
1136 WerrorS("boxSet: index out of range");
1137 return TRUE;
1138 }
1139
1140 box *RES = new box(B);
1141
1142 RES->setInterval(i-1, new interval(I));
1143 // ensure consistency
1144 RES->intervals[i-1]->setRing(RES->R);
1145
1146 result->rtyp = boxID;
1147 result->data = (void*) RES;
1148 args->CleanUp();
1149 return FALSE;
1150}
BOOLEAN iiCheckTypes(leftv args, const short *type_list, int report)
check a list of arguemys against a given field of types return TRUE if the types match return FALSE (...
Definition: ipshell.cc:6572

◆ evalPolyAtBox()

static BOOLEAN evalPolyAtBox ( leftv  result,
leftv  args 
)
static

Definition at line 1156 of file interval.cc.

1157{
1158 const short t[] = {2, POLY_CMD, (short) boxID};
1159 if (!iiCheckTypes(args, t, 1))
1160 {
1161 return TRUE;
1162 }
1163
1164 poly p = (poly) args->Data();
1165 box *B = (box*) args->next->Data();
1166 int i, pot, n = B->R->N;
1167
1168 interval *tmp, *tmpPot, *tmpMonom, *RES = new interval();
1169
1170 while(p != NULL)
1171 {
1172 tmpMonom = new interval(nInit(1));
1173
1174 for (i = 1; i <= n; i++)
1175 {
1176 pot = pGetExp(p, i);
1177
1178 tmpPot = intervalPower(B->intervals[i-1], pot);
1179 tmp = intervalMultiply(tmpMonom, tmpPot);
1180
1181 delete tmpMonom;
1182 delete tmpPot;
1183
1184 tmpMonom = tmp;
1185 }
1186
1187 tmp = intervalScalarMultiply(p->coef, tmpMonom);
1188 delete tmpMonom;
1189 tmpMonom = tmp;
1190
1191 tmp = intervalAdd(RES, tmpMonom);
1192 delete RES;
1193 delete tmpMonom;
1194
1195 RES = tmp;
1196
1197 p = p->next;
1198 }
1199
1200 if (result->Data() != NULL)
1201 {
1202 delete (box*) result->Data();
1203 }
1204
1205 result->rtyp = intervalID;
1206 result->data = (void*) RES;
1207 args->CleanUp();
1208 return FALSE;
1209}
int p
Definition: cfModGcd.cc:4078
@ POLY_CMD
Definition: grammar.cc:289
static interval * intervalAdd(interval *I, interval *J)
Definition: interval.cc:332
static interval * intervalScalarMultiply(number a, interval *I)
Definition: interval.cc:274
static interval * intervalPower(interval *I, int p)
Definition: interval.cc:372
static interval * intervalMultiply(interval *I, interval *J)
Definition: interval.cc:295
#define nInit(i)
Definition: numbers.h:24
#define pGetExp(p, i)
Exponent.
Definition: polys.h:41

◆ interval()

int SI_MOD_INIT() interval ( SModulFunctions psModulFunctions)

Definition at line 1215 of file interval.cc.

1216{
1217 blackbox *b_iv = (blackbox*) omAlloc0(sizeof(blackbox)),
1218 *b_bx = (blackbox*) omAlloc0(sizeof(blackbox));
1219
1220 b_iv->blackbox_Init = interval_Init;
1221 b_iv->blackbox_Copy = interval_Copy;
1222 b_iv->blackbox_destroy = interval_Destroy;
1223 b_iv->blackbox_String = interval_String;
1224 b_iv->blackbox_Assign = interval_Assign;
1225 b_iv->blackbox_Op2 = interval_Op2;
1226 b_iv->blackbox_serialize = interval_serialize;
1227 b_iv->blackbox_deserialize = interval_deserialize;
1228
1229 intervalID = setBlackboxStuff(b_iv, "interval");
1230
1231 b_bx->blackbox_Init = box_Init;
1232 b_bx->blackbox_Copy = box_Copy;
1233 b_bx->blackbox_destroy = box_Destroy;
1234 b_bx->blackbox_String = box_String;
1235 b_bx->blackbox_Assign = box_Assign;
1236 b_bx->blackbox_Op2 = box_Op2;
1237 b_bx->blackbox_OpM = box_OpM;
1238 b_bx->blackbox_serialize = box_serialize;
1239 b_bx->blackbox_deserialize = box_deserialize;
1240
1241 boxID = setBlackboxStuff(b_bx, "box");
1242
1243 // add additional functions
1244 psModulFunctions->iiAddCproc("rootisolation.lib", "length", FALSE, length);
1245 psModulFunctions->iiAddCproc("rootisolation.lib", "boxSet", FALSE, boxSet);
1246 psModulFunctions->iiAddCproc("rootisolation.lib", "evalPolyAtBox", FALSE,
1248
1249 return MAX_TOK;
1250}
int setBlackboxStuff(blackbox *bb, const char *n)
define a new type
Definition: blackbox.cc:142
static BOOLEAN box_serialize(blackbox *, void *d, si_link f)
Definition: interval.cc:1066
static BOOLEAN evalPolyAtBox(leftv result, leftv args)
Definition: interval.cc:1156
static void * box_Init(blackbox *)
Definition: interval.cc:776
static BOOLEAN box_Assign(leftv result, leftv args)
Definition: interval.cc:815
static void interval_Destroy(blackbox *, void *d)
Definition: interval.cc:171
static void box_Destroy(blackbox *, void *d)
Definition: interval.cc:786
static void * interval_Init(blackbox *)
Definition: interval.cc:137
static BOOLEAN box_OpM(int op, leftv result, leftv args)
Definition: interval.cc:994
static char * box_String(blackbox *, void *d)
Definition: interval.cc:792
static void * interval_Copy(blackbox *, void *d)
Definition: interval.cc:165
static BOOLEAN interval_serialize(blackbox *, void *d, si_link f)
Definition: interval.cc:720
static BOOLEAN interval_Assign(leftv result, leftv args)
Definition: interval.cc:178
static BOOLEAN interval_Op2(int op, leftv result, leftv i1, leftv i2)
Definition: interval.cc:432
static void * box_Copy(blackbox *, void *d)
Definition: interval.cc:781
static BOOLEAN interval_deserialize(blackbox **, void **d, si_link f)
Definition: interval.cc:756
static BOOLEAN box_deserialize(blackbox **, void **d, si_link f)
Definition: interval.cc:1096
static BOOLEAN box_Op2(int op, leftv result, leftv b1, leftv b2)
Definition: interval.cc:884
static BOOLEAN length(leftv result, leftv arg)
Definition: interval.cc:257
static BOOLEAN boxSet(leftv result, leftv args)
Definition: interval.cc:1121
#define omAlloc0(size)
Definition: omAllocDecl.h:211
int(* iiAddCproc)(const char *libname, const char *procname, BOOLEAN pstatic, BOOLEAN(*func)(leftv res, leftv v))
Definition: ipid.h:70
@ MAX_TOK
Definition: tok.h:218

◆ interval_Assign()

static BOOLEAN interval_Assign ( leftv  result,
leftv  args 
)
static

Definition at line 178 of file interval.cc.

179{
180 interval *RES;
181
182 /*
183 * Allow assignments of the form
184 * I = a,
185 * I = a, b,
186 * I = J
187 * where a, b numbers or ints and J interval
188 */
189
190 if (args->Typ() == intervalID)
191 {
192 RES = new interval((interval*) args->CopyD());
193 }
194 else
195 {
196 number n1, n2;
197
198 if (args->Typ() == INT_CMD)
199 {
200 n1 = nInit((int)(long) args->Data());
201 }
202 else if (args->Typ() == NUMBER_CMD)
203 {
204 n1 = (number) args->CopyD();
205 }
206 else
207 {
208 WerrorS("Input not supported: first argument not int or number");
209 return TRUE;
210 }
211
212 // check if second argument exists
213 if (args->next == NULL)
214 {
215 RES = new interval(n1);
216 }
217 else
218 {
219 if (args->next->Typ() == INT_CMD)
220 {
221 n2 = nInit((int)(long) args->next->Data());
222 }
223 else if (args->next->Typ() == NUMBER_CMD)
224 {
225 n2 = (number) args->next->CopyD();
226 }
227 else
228 {
229 WerrorS("Input not supported: second argument not int or number");
230 return TRUE;
231 }
232
233 RES = new interval(n1, n2);
234 }
235 }
236
237 // destroy data of result if it exists
238 if (result->Data() != NULL)
239 {
240 delete (interval*) result->Data();
241 }
242
243 if (result->rtyp == IDHDL)
244 {
245 IDDATA((idhdl)result->data) = (char*) RES;
246 }
247 else
248 {
249 result->rtyp = intervalID;
250 result->data = (void*) RES;
251 }
252
253 args->CleanUp();
254 return FALSE;
255}
void * CopyD(int t)
Definition: subexpr.cc:710
@ NUMBER_CMD
Definition: grammar.cc:288

◆ interval_Copy()

static void * interval_Copy ( blackbox *  ,
void *  d 
)
static

Definition at line 165 of file interval.cc.

166{
167 return (void*) new interval((interval*) d);
168}

◆ interval_deserialize()

static BOOLEAN interval_deserialize ( blackbox **  ,
void **  d,
si_link  f 
)
static

Definition at line 756 of file interval.cc.

757{
758 leftv l;
759
760 l = f->m->Read(f);
761 l->next = f->m->Read(f);
762
763 number lo = (number) l->CopyD(),
764 up = (number) l->next->CopyD();
765
766 l->CleanUp();
767
768 *d = (void*) new interval(lo, up);
769 return FALSE;
770}

◆ interval_Destroy()

static void interval_Destroy ( blackbox *  ,
void *  d 
)
static

Definition at line 171 of file interval.cc.

172{
173 if (d != NULL)
174 delete (interval*) d;
175}

◆ interval_Init()

static void * interval_Init ( blackbox *  )
static

Definition at line 137 of file interval.cc.

138{
139 return (void*) new interval();
140}

◆ interval_Op2()

static BOOLEAN interval_Op2 ( int  op,
leftv  result,
leftv  i1,
leftv  i2 
)
static

Definition at line 432 of file interval.cc.

433{
434 interval *RES;
435
436 switch(op)
437 {
438 case '+':
439 {
440 if (i1->Typ() != intervalID || i2->Typ() != intervalID)
441 {
442 WerrorS("syntax: <interval> + <interval>");
443 return TRUE;
444 }
445 interval *I1, *I2;
446 I1 = (interval*) i1->Data();
447 I2 = (interval*) i2->Data();
448 if (I1->R != I2->R)
449 {
450 WerrorS("adding intervals defined in different rings not supported");
451 return TRUE;
452 }
453
454 RES = intervalAdd(I1, I2);
455 break;
456 }
457 case '-':
458 {
459 if (i1->Typ() != intervalID || i2->Typ() != intervalID)
460 {
461 WerrorS("syntax: <interval> - <interval>");
462 return TRUE;
463 }
464 interval *I1, *I2;
465 I1 = (interval*) i1->Data();
466 I2 = (interval*) i2->Data();
467 if (I1->R != I2->R)
468 {
469 WerrorS("subtracting intervals defined in different rings not supported");
470 return TRUE;
471 }
472
473 RES = intervalSubtract(I1, I2);
474 break;
475 }
476 case '*':
477 {
478 if (i1->Typ() == i2->Typ())
479 {
480 // both must be intervals
481 interval *I1, *I2;
482 I1 = (interval*) i1->Data();
483 I2 = (interval*) i2->Data();
484 if (I1->R != I2->R)
485 {
486 WerrorS("multiplying intervals defined in different rings not supported");
487 return TRUE;
488 }
489
490 RES = intervalMultiply(I1, I2);
491 }
492 else
493 {
494 // one arg is scalar, one is interval
495 // give new names to reduce to one case
496 leftv iscalar, iinterv;
497 if (i1->Typ() != intervalID)
498 {
499 // i1 is scalar
500 iscalar = i1;
501 iinterv = i2;
502 }
503 else
504 {
505 // i2 is scalar
506 iscalar = i2;
507 iinterv = i1;
508 }
509 number n;
510
511 switch (iscalar->Typ())
512 {
513 case INT_CMD:
514 { n = nInit((int)(long) iscalar->Data()); break; }
515 case NUMBER_CMD:
516 { n = (number) iscalar->CopyD(); break; }
517 default:
518 { WerrorS("first argument not int/number/interval"); return TRUE; }
519 }
520
521 interval *I = (interval*) iinterv->Data();
522
523 RES = intervalScalarMultiply(n, I);
524 // n no longer needed, delete it
525 nDelete(&n);
526 }
527
528 break;
529 }
530 case '/':
531 {
532 if(i2->Typ() == intervalID)
533 {
534 interval *I2;
535 I2 = (interval*) i2->Data();
536
537 // make sure I2 is invertible
538 if (intervalContainsZero(I2))
539 {
540 WerrorS("second interval contains zero");
541 return TRUE;
542 }
543
544 number invlo, invup;
545 invlo = n_Invers(I2->lower, I2->R->cf);
546 invup = n_Invers(I2->upper, I2->R->cf);
547
548 // inverse interval
549 interval *I2inv = new interval(invup, invlo, I2->R);
550
551 if (i1->Typ() == intervalID)
552 {
553 interval *I1 = (interval*) i1->Data();
554 if (I1->R != I2->R)
555 {
556 WerrorS("dividing intervals from different rings not supported");
557 delete I2inv;
558 return TRUE;
559 }
560 RES = intervalMultiply(I1, I2inv);
561 }
562 else
563 {
564 // i1 is not an interval
565 number n;
566 switch (i1->Typ())
567 {
568 case INT_CMD:
569 {
570 n = nInit((int)(long) i1->Data());
571 break;
572 }
573 case NUMBER_CMD:
574 {
575 n = nCopy((number) i1->Data());
576 break;
577 }
578 default:
579 {
580 WerrorS("first argument not int/number/interval");
581 delete I2inv;
582 return TRUE;
583 }
584 }
585 RES = intervalScalarMultiply(n, I2inv);
586 nDelete(&n);
587 }
588
589 delete I2inv;
590 break;
591 }
592 else
593 {
594 // i2 is not an interval
595 interval *I1 = (interval*) i1->Data();
596 number n;
597 switch(i2->Typ())
598 {
599 case INT_CMD:
600 { n = nInit((int)(long) i2->Data()); break; }
601 case NUMBER_CMD:
602 { n = nCopy((number) i2->Data()); break; }
603 default:
604 {
605 WerrorS("second argument not int/number/interval");
606 return TRUE;
607 }
608 }
609 // handle zero to prevent memory leak (?)
610 if (nIsZero(n))
611 {
612 WerrorS("<interval>/0 not supported");
613 return TRUE;
614 }
615
616 number nInv = nInvers(n);
617 nDelete(&n);
618 RES = intervalScalarMultiply(nInv, I1);
619 nDelete(&nInv);
620
621 break;
622 }
623
624 break;
625 }
626 case '^':
627 {
628 if (i1->Typ() != intervalID || i2->Typ() != INT_CMD)
629 {
630 WerrorS("syntax: <interval> ^ <int>");
631 return TRUE;
632 }
633 int p = (int)(long) i2->Data();
634 if (p < 0)
635 {
636 WerrorS("<interval> ^ n not implemented for n < 0");
637 return TRUE;
638 }
639 interval *I = (interval*) i1->Data();
640
641 RES = intervalPower(I, p);
642 break;
643 }
644 case EQUAL_EQUAL:
645 {
646 if (i1->Typ() != intervalID || i2->Typ() != intervalID)
647 {
648 WerrorS("syntax: <interval> == <interval>");
649 return TRUE;
650 }
651 interval *I1, *I2;
652 I1 = (interval*) i1->Data();
653 I2 = (interval*) i2->Data();
654
655 result->rtyp = INT_CMD;
656 result->data = (void*) intervalEqual(I1, I2);
657 i1->CleanUp();
658 i2->CleanUp();
659 return FALSE;
660 }
661 case '[':
662 {
663 if (i1->Typ() != intervalID || i2->Typ() != INT_CMD)
664 {
665 WerrorS("syntax: <interval>[<int>]");
666 return TRUE;
667 }
668
669 interval *I = (interval*) i1->Data();
670 int n = (int)(long) i2->Data();
671
672 number out;
673 if (n == 1)
674 {
675 out = nCopy(I->lower);
676 }
677 else if (n == 2)
678 {
679 out = nCopy(I->upper);
680 }
681 else
682 {
683 WerrorS("Allowed indices are 1 and 2");
684 return TRUE;
685 }
686
687 // delete number in result
688 if (result != NULL && result->Data() != NULL)
689 {
690 number r = (number) result->Data();
691 nDelete(&r);
692 }
693
694 result->rtyp = NUMBER_CMD;
695 result->data = (void*) out;
696 i1->CleanUp();
697 i2->CleanUp();
698 return FALSE;
699 }
700 default:
701 {
702 // use default error
703 return blackboxDefaultOp2(op, result, i1, i2);
704 }
705 }
706
707 // destroy data of result if it exists
708 if (result->Data() != NULL)
709 {
710 delete (interval*) result->Data();
711 }
712
713 result->rtyp = intervalID;
714 result->data = (void*) RES;
715 i1->CleanUp();
716 i2->CleanUp();
717 return FALSE;
718}
static FORCE_INLINE number n_Invers(number a, const coeffs r)
return the multiplicative inverse of 'a'; raise an error if 'a' is not invertible
Definition: coeffs.h:561
static bool intervalContainsZero(interval *I)
Definition: interval.cc:362
#define nDelete(n)
Definition: numbers.h:16
#define nIsZero(n)
Definition: numbers.h:19
#define nInvers(a)
Definition: numbers.h:33
number lower
Definition: interval.h:8
ring R
Definition: interval.h:10
number upper
Definition: interval.h:9

◆ interval_serialize()

static BOOLEAN interval_serialize ( blackbox *  ,
void *  d,
si_link  f 
)
static

Definition at line 720 of file interval.cc.

721{
722 /*
723 * Format: "interval" setring number number
724 */
725 interval *I = (interval*) d;
726
727 sleftv l, lo, up;
728 memset(&l, 0, sizeof(l));
729 memset(&lo, 0, sizeof(lo));
730 memset(&up, 0, sizeof(up));
731
732 l.rtyp = STRING_CMD;
733 l.data = (void*) "interval";
734
735 lo.rtyp = NUMBER_CMD;
736 lo.data = (void*) I->lower;
737
738 up.rtyp = NUMBER_CMD;
739 up.data = (void*) I->upper;
740
741 f->m->Write(f, &l);
742
743 ring R = I->R;
744
745 f->m->SetRing(f, R, TRUE);
746 f->m->Write(f, &lo);
747 f->m->Write(f, &up);
748
749 // no idea
750 if (R != currRing)
751 f->m->SetRing(f, currRing, FALSE);
752
753 return FALSE;
754}

◆ interval_String()

static char * interval_String ( blackbox *  ,
void *  d 
)
static

Definition at line 143 of file interval.cc.

144{
145 if (d == NULL)
146 {
147 // invalid object
148 return omStrDup("[?]");
149 }
150 else
151 {
152 interval* i = (interval*) d;
153
154 // use n_Write since nothing better (?) exists
155 StringSetS("[");
156 n_Write(i->lower, i->R->cf);
157 StringAppendS(", ");
158 n_Write(i->upper, i->R->cf);
159 StringAppendS("]");
160
161 return StringEndS();
162 }
163}
static FORCE_INLINE void n_Write(number n, const coeffs r, const BOOLEAN bShortOut=TRUE)
Definition: coeffs.h:588

◆ intervalAdd()

static interval * intervalAdd ( interval I,
interval J 
)
static

Definition at line 332 of file interval.cc.

333{
334 number lo = n_Add(I->lower, J->lower, I->R->cf),
335 up = n_Add(I->upper, J->upper, I->R->cf);
336
337 n_Normalize(lo, I->R->cf);
338 n_Normalize(up, I->R->cf);
339
340 return new interval(lo, up);
341}
static FORCE_INLINE number n_Add(number a, number b, const coeffs r)
return the sum of 'a' and 'b', i.e., a+b
Definition: coeffs.h:647
static FORCE_INLINE void n_Normalize(number &n, const coeffs r)
inplace-normalization of n; produces some canonical representation of n;
Definition: coeffs.h:575

◆ intervalContainsZero()

static bool intervalContainsZero ( interval I)
static

Definition at line 362 of file interval.cc.

363{
364 number n = n_Mult(I->lower, I->upper, I->R->cf);
365 bool result = !n_GreaterZero(n, I->R->cf);
366 // delete helper number
367 n_Delete(&n, I->R->cf);
368
369 return result;
370}
static FORCE_INLINE number n_Mult(number a, number b, const coeffs r)
return the product of 'a' and 'b', i.e., a*b
Definition: coeffs.h:633
static FORCE_INLINE BOOLEAN n_GreaterZero(number n, const coeffs r)
ordered fields: TRUE iff 'n' is positive; in Z/pZ: TRUE iff 0 < m <= roundedBelow(p/2),...
Definition: coeffs.h:491
static FORCE_INLINE void n_Delete(number *p, const coeffs r)
delete 'p'
Definition: coeffs.h:452

◆ intervalEqual()

static bool intervalEqual ( interval I,
interval J 
)
static

Definition at line 354 of file interval.cc.

355{
356 assume(I->R == J->R);
357 return n_Equal(I->lower, J->lower, I->R->cf)
358 && n_Equal(I->upper, J->upper, I->R->cf);
359}
static FORCE_INLINE BOOLEAN n_Equal(number a, number b, const coeffs r)
TRUE iff 'a' and 'b' represent the same number; they may have different representations.
Definition: coeffs.h:457
#define assume(x)
Definition: mod2.h:389

◆ intervalMultiply()

static interval * intervalMultiply ( interval I,
interval J 
)
static

Definition at line 295 of file interval.cc.

296{
297 number lo, up;
298 number nums[4];
299 nums[0] = n_Mult(I->lower, J->lower, I->R->cf);
300 nums[1] = n_Mult(I->lower, J->upper, I->R->cf);
301 nums[2] = n_Mult(I->upper, J->lower, I->R->cf);
302 nums[3] = n_Mult(I->upper, J->upper, I->R->cf);
303
304 int i, imax = 0, imin = 0;
305 for (i = 1; i < 4; i++)
306 {
307 if (n_Greater(nums[i], nums[imax], I->R->cf))
308 {
309 imax = i;
310 }
311 if (n_Greater(nums[imin], nums[i], I->R->cf))
312 {
313 imin = i;
314 }
315 }
316
317 lo = n_Copy(nums[imin], I->R->cf);
318 up = n_Copy(nums[imax], I->R->cf);
319
320 // delete products
321 for (i = 0; i < 4; i++)
322 {
323 n_Delete(&nums[i], I->R->cf);
324 }
325
326 n_Normalize(lo, I->R->cf);
327 n_Normalize(up, I->R->cf);
328
329 return new interval(lo, up, I->R);
330}
static FORCE_INLINE number n_Copy(number n, const coeffs r)
return a copy of 'n'
Definition: coeffs.h:448
static FORCE_INLINE BOOLEAN n_Greater(number a, number b, const coeffs r)
ordered fields: TRUE iff 'a' is larger than 'b'; in Z/pZ: TRUE iff la > lb, where la and lb are the l...
Definition: coeffs.h:508

◆ intervalPower()

static interval * intervalPower ( interval I,
int  p 
)
static

Definition at line 372 of file interval.cc.

373{
374 if (p == 0)
375 {
376 return new interval(n_Init(1,I->R->cf), I->R);
377 }
378
379 // no initialisation required (?)
380 number lo, up;
381
382 n_Power(I->lower, p, &lo, I->R->cf);
383 n_Power(I->upper, p, &up, I->R->cf);
384
385 // should work now
386 if (p % 2 == 1)
387 {
388 return new interval(lo, up, I->R);
389 }
390 else
391 {
392 // perform pointer swap if necessary
393 number tmp;
394 if (n_Greater(lo, up, I->R->cf))
395 {
396 tmp = up;
397 up = lo;
398 lo = tmp;
399 }
400
402 {
403 n_Delete(&lo, I->R->cf);
404 lo = n_Init(0, I->R->cf);
405 }
406 return new interval(lo, up, I->R);
407 }
408}
static FORCE_INLINE void n_Power(number a, int b, number *res, const coeffs r)
fill res with the power a^b
Definition: coeffs.h:629
static FORCE_INLINE number n_Init(long i, const coeffs r)
a number representing i in the given coeff field/ring r
Definition: coeffs.h:535

◆ intervalScalarMultiply()

static interval * intervalScalarMultiply ( number  a,
interval I 
)
static

Definition at line 274 of file interval.cc.

275{
276 // must assume a is in ring I->R
277 number lo, up;
278 if (nGreaterZero(a))
279 {
280 lo = n_Mult(a, I->lower, I->R->cf);
281 up = n_Mult(a, I->upper, I->R->cf);
282 }
283 else
284 {
285 lo = n_Mult(a, I->upper, I->R->cf);
286 up = n_Mult(a, I->lower, I->R->cf);
287 }
288
289 n_Normalize(lo, I->R->cf);
290 n_Normalize(up, I->R->cf);
291
292 return new interval(lo, up, I->R);
293}
#define nGreaterZero(n)
Definition: numbers.h:27

◆ intervalSubtract()

static interval * intervalSubtract ( interval I,
interval J 
)
static

Definition at line 343 of file interval.cc.

344{
345 number lo = n_Sub(I->lower, J->upper, I->R->cf),
346 up = n_Sub(I->upper, J->lower, I->R->cf);
347
348 n_Normalize(lo, I->R->cf);
349 n_Normalize(up, I->R->cf);
350
351 return new interval(lo, up, I->R);
352}
static FORCE_INLINE number n_Sub(number a, number b, const coeffs r)
return the difference of 'a' and 'b', i.e., a-b
Definition: coeffs.h:652

◆ length()

static BOOLEAN length ( leftv  result,
leftv  arg 
)
static

Definition at line 257 of file interval.cc.

258{
259 if (arg != NULL && arg->Typ() == intervalID)
260 {
261 interval *I = (interval*) arg->Data();
262 result->rtyp = NUMBER_CMD;
263 result->data = (void*) n_Sub(I->upper, I->lower, I->R->cf);
264 arg->CleanUp();
265 return FALSE;
266 }
267
268 WerrorS("syntax: length(<interval>)");
269 return TRUE;
270}

Variable Documentation

◆ boxID

STATIC_VAR int boxID

Definition at line 131 of file interval.cc.

◆ intervalID

STATIC_VAR int intervalID

Definition at line 130 of file interval.cc.