source: git/libpolys/coeffs/gnumpc.cc @ 0afa07

spielwiese
Last change on this file since 0afa07 was 0afa07, checked in by Frank Seelisch <seelisch@…>, 13 years ago
removed cfPar, cfParDeg, n_Par, n_ParDeg, ndPar, ndParDeg from coeffs
  • Property mode set to 100644
File size: 11.4 KB
RevLine 
[35aab3]1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
[341696]4/* $Id$ */
[35aab3]5/*
6* ABSTRACT: computations with GMP complex floating-point numbers
7*
8* ngc == number gnu complex
9*/
10
[691dba]11#include "config.h"
[c3d175]12
[2d805a]13#include <coeffs/coeffs.h>
14#include <coeffs/numbers.h>
15#include <coeffs/longrat.h>
16#include <coeffs/modulop.h>
17#include <coeffs/gnumpc.h>
18#include <coeffs/gnumpfl.h>
19#include <coeffs/mpr_complex.h>
[31213a4]20#include <reporter/reporter.h>
21#include <omalloc/omalloc.h>
[35aab3]22
23
[2d805a]24#include <coeffs/shortfl.h>
[3aae0e]25
[51d835]26/// Our Type!
27static const n_coeffType ID = n_long_C;
28
[3aae0e]29
[94b759]30#ifdef LDEBUG
31// not yet implemented
[9bb5457]32BOOLEAN ngcDBTest(number, const char *, const int, const coeffs r)
[94b759]33{
34  assume( getCoeffType(r) == ID );
35
36  return TRUE;
37}
38#endif
39
[0afa07]40/*
[94b759]41number   ngcPar(int i, const coeffs r)
[35aab3]42{
[94b759]43  assume( getCoeffType(r) == ID );
[121fd9]44  assume(i==1);
[94b759]45
[35aab3]46  gmp_complex* n= new gmp_complex( (long)0, (long)1 );
47  return (number)n;
48}
[0afa07]49*/
[35aab3]50
51/*2
52* n := i
53*/
[7d90aa]54number ngcInit (int i, const coeffs r)
[35aab3]55{
[94b759]56  assume( getCoeffType(r) == ID );
[9bb5457]57
[78e67e]58  gmp_complex* n= new gmp_complex( (long)i, (long)0 );
[9bb5457]59
[35aab3]60  return (number)n;
61}
62
63/*2
64* convert number to int
65*/
[7d90aa]66int ngcInt(number &i, const coeffs r)
[35aab3]67{
[94b759]68  assume( getCoeffType(r) == ID );
[9bb5457]69
[35aab3]70  return (int)((gmp_complex*)i)->real();
71}
72
[95d8df]73int ngcSize(number n, const coeffs R)
[12cca3]74{
75  int r = (int)((gmp_complex*)n)->real();
76  if (r < 0) r = -r;
77  int i = (int)((gmp_complex*)n)->imag();
78  if (i < 0) i = -i;
79  int oneNorm = r + i;
80  /* basically return the 1-norm of n;
81     only if this happens to be zero although n != 0,
82     return 1;
83     (this code ensures that zero has the size zero) */
[95d8df]84  if ((oneNorm == 0.0) & (ngcIsZero(n,R) == FALSE)) oneNorm = 1;
[12cca3]85  return oneNorm;
86}
87
[35aab3]88/*2
89* delete a
90*/
[7d90aa]91void ngcDelete (number * a, const coeffs r)
[35aab3]92{
[94b759]93  assume( getCoeffType(r) == ID );
94
[7c961ba]95  if ( *a != NULL )
96  {
[35aab3]97    delete *(gmp_complex**)a;
98    *a=NULL;
99  }
100}
101
102/*2
[8cb8e1]103 * copy a to b
[35aab3]104*/
[94b759]105number ngcCopy(number a, const coeffs r)
[35aab3]106{
[94b759]107  assume( getCoeffType(r) == ID );
[9bb5457]108
[78e67e]109  gmp_complex* b= new gmp_complex( *(gmp_complex*)a );
[35aab3]110  return (number)b;
111}
[94b759]112
[35aab3]113
114/*2
115* za:= - za
116*/
[94b759]117number ngcNeg (number a, const coeffs R)
[35aab3]118{
[94b759]119  assume( getCoeffType(R) == ID );
120
[0f4db3]121  gmp_complex* r=(gmp_complex*)a;
[95d8df]122  (*r).neg();
[35aab3]123  return (number)a;
124}
125
126/*
127* 1/a
128*/
[94b759]129number ngcInvers(number a, const coeffs R)
[35aab3]130{
[94b759]131  assume( getCoeffType(R) == ID );
132
[b7e838]133  gmp_complex* r = NULL;
[78e67e]134  if (((gmp_complex*)a)->isZero())
[35aab3]135  {
[b7e838]136    WerrorS(nDivBy0);
[35aab3]137  }
138  else
139  {
[95d8df]140    r = new gmp_complex( (gmp_complex)1 / (*(gmp_complex*)a) );
[35aab3]141  }
[95d8df]142  return (number)r;
[35aab3]143}
144
145/*2
146* u:= a + b
147*/
[94b759]148number ngcAdd (number a, number b, const coeffs R)
[35aab3]149{
[94b759]150  assume( getCoeffType(R) == ID );
151
[78e67e]152  gmp_complex* r= new gmp_complex( (*(gmp_complex*)a) + (*(gmp_complex*)b) );
[35aab3]153  return (number)r;
154}
155
156/*2
157* u:= a - b
158*/
[8cb8e1]159number ngcSub (number a, number b, const coeffs R)
[35aab3]160{
[94b759]161  assume( getCoeffType(R) == ID );
162
[78e67e]163  gmp_complex* r= new gmp_complex( (*(gmp_complex*)a) - (*(gmp_complex*)b) );
[35aab3]164  return (number)r;
165}
166
167/*2
168* u := a * b
169*/
[94b759]170number ngcMult (number a, number b, const coeffs R)
[35aab3]171{
[94b759]172  assume( getCoeffType(R) == ID );
[9bb5457]173
[78e67e]174  gmp_complex* r= new gmp_complex( (*(gmp_complex*)a) * (*(gmp_complex*)b) );
[35aab3]175  return (number)r;
176}
177
178/*2
179* u := a / b
180*/
[94b759]181number ngcDiv (number a, number b, const coeffs r)
[35aab3]182{
[94b759]183  assume( getCoeffType(r) == ID );
184
[78e67e]185  if (((gmp_complex*)b)->isZero())
[35aab3]186  {
[b7e838]187    // a/0 = error
188    WerrorS(nDivBy0);
[35aab3]189    return NULL;
190  }
[94b759]191  gmp_complex* res = new gmp_complex( (*(gmp_complex*)a) / (*(gmp_complex*)b) );
192  return (number)res;
[35aab3]193}
194
195/*2
196* u:= x ^ exp
197*/
[94b759]198void ngcPower ( number x, int exp, number * u, const coeffs r)
[35aab3]199{
[94b759]200  assume( getCoeffType(r) == ID );
201
[35aab3]202  if ( exp == 0 )
203  {
204    gmp_complex* n = new gmp_complex(1);
205    *u=(number)n;
206    return;
207  }
[5fe545]208  else if ( exp == 1 )
[35aab3]209  {
[94b759]210    n_New(u, r);
[78e67e]211    gmp_complex* n = new gmp_complex();
212    *n= *(gmp_complex*)x;
213    *u=(number)n;
[35aab3]214    return;
215  }
[5fe545]216  else if (exp == 2)
217  {
[94b759]218    n_New(u, r);
[78e67e]219    gmp_complex* n = new gmp_complex();
220    *n= *(gmp_complex*)x;
221    *u=(number)n;
222    *(gmp_complex*)(*u) *= *(gmp_complex*)n;
[5fe545]223    return;
224  }
[039016]225  if ( (exp & 1) == 1 )
[5fe545]226  {
[94b759]227    ngcPower(x,exp-1,u, r);
228    gmp_complex *n = new gmp_complex();
[5fe545]229    *n=*(gmp_complex*)x;
230    *(gmp_complex*)(*u) *= *(gmp_complex*)n;
231    delete n;
232  }
233  else
234  {
235    number w;
[94b759]236    n_New(&w, r);
237    ngcPower(x,exp/2,&w, r);
238    ngcPower(w,2,u, r);
239    n_Delete(&w, r);
[5fe545]240  }
[35aab3]241}
242
[94b759]243BOOLEAN ngcIsZero (number a, const coeffs r)
[35aab3]244{
[94b759]245  assume( getCoeffType(r) == ID );
246
[35aab3]247  return ( ((gmp_complex*)a)->real().isZero() && ((gmp_complex*)a)->imag().isZero());
248}
249
[94b759]250number ngcRePart(number a, const coeffs r)
[35aab3]251{
[94b759]252  assume( getCoeffType(r) == ID );
253
[35aab3]254  gmp_complex* n = new gmp_complex(((gmp_complex*)a)->real());
255  return (number)n;
256}
257
[94b759]258number ngcImPart(number a, const coeffs r)
[35aab3]259{
[94b759]260  assume( getCoeffType(r) == ID );
261
[35aab3]262  gmp_complex* n = new gmp_complex(((gmp_complex*)a)->imag());
263  return (number)n;
264}
265
266/*2
267* za >= 0 ?
268*/
[94b759]269BOOLEAN ngcGreaterZero (number a, const coeffs r)
[35aab3]270{
[94b759]271  assume( getCoeffType(r) == ID );
272
[35aab3]273  if ( ! ((gmp_complex*)a)->imag().isZero() )
274    return ( abs( *(gmp_complex*)a).sign() >= 0 );
275  else
276    return ( ((gmp_complex*)a)->real().sign() >= 0 );
277}
278
279/*2
280* a > b ?
281*/
[94b759]282BOOLEAN ngcGreater (number a, number b, const coeffs r)
[35aab3]283{
[94b759]284  assume( getCoeffType(r) == ID );
285
[890d63b]286  gmp_complex *aa=(gmp_complex*)a;
287  gmp_complex *bb=(gmp_complex*)b;
288  return (*aa) > (*bb);
[35aab3]289}
290
291/*2
292* a = b ?
293*/
[94b759]294BOOLEAN ngcEqual (number a, number b, const coeffs r)
[35aab3]295{
[94b759]296  assume( getCoeffType(r) == ID );
297
[890d63b]298  gmp_complex *aa=(gmp_complex*)a;
299  gmp_complex *bb=(gmp_complex*)b;
300  return (*aa) == (*bb);
[35aab3]301}
302
303/*2
304* a == 1 ?
305*/
[94b759]306BOOLEAN ngcIsOne (number a, const coeffs r)
[35aab3]307{
[94b759]308  assume( getCoeffType(r) == ID );
309
[78e67e]310  return (((gmp_complex*)a)->real().isOne() && ((gmp_complex*)a)->imag().isZero());
311  //return (((gmp_complex*)a)->real().isOne());
[35aab3]312}
313
314/*2
315* a == -1 ?
316*/
[94b759]317BOOLEAN ngcIsMOne (number a, const coeffs r)
[35aab3]318{
[94b759]319  assume( getCoeffType(r) == ID );
320
[78e67e]321  return (((gmp_complex*)a)->real().isMOne() && ((gmp_complex*)a)->imag().isZero());
322  //return (((gmp_complex*)a)->real().isMOne());
[35aab3]323}
324
325/*2
326* extracts the number a from s, returns the rest
327*/
[94b759]328const char * ngcRead (const char * s, number * a, const coeffs r)
[35aab3]329{
[94b759]330  assume( getCoeffType(r) == ID );
[bfa04e]331  assume( r->complex_parameter != NULL );
332
[35aab3]333  if ((*s >= '0') && (*s <= '9'))
334  {
335    gmp_float *re=NULL;
[94b759]336    s=ngfRead(s,(number *)&re, r);
[35aab3]337    gmp_complex *aa=new gmp_complex(*re);
338    *a=(number)aa;
339    delete re;
340  }
[bfa04e]341  else if (strncmp(s, r->complex_parameter,strlen(r->complex_parameter))==0)
[35aab3]342  {
[bfa04e]343    s+=strlen(r->complex_parameter);
[35aab3]344    gmp_complex *aa=new gmp_complex((long)0,(long)1);
345    *a=(number)aa;
346  }
347  else
348  {
349    *a=(number) new gmp_complex((long)1);
350  }
351  return s;
352}
353
[94b759]354
355
[35aab3]356/*2
357* write a floating point number
358*/
[7d90aa]359void ngcWrite (number &a, const coeffs r)
[35aab3]360{
[94b759]361  assume( getCoeffType(r) == ID );
362
363  extern size_t gmp_output_digits; /// comes from mpr_complex.cc
364
[35aab3]365  if (a==NULL)
[e53182]366    StringAppendS("0");
[35aab3]367  else
368  {
369    char *out;
[ac5686]370    out= complexToStr(*(gmp_complex*)a, gmp_output_digits, r);
[e53182]371    StringAppendS(out);
[7d90aa]372    //    omFreeSize((void *)out, (strlen(out)+1)* sizeof(char) );
373    omFree( (void *)out );
[35aab3]374  }
375}
376
[4c6e420]377BOOLEAN ngcInitChar(coeffs n, void* p)
[51d835]378{
379  assume( getCoeffType(n) == ID );
[2544e7]380  n->cfKillChar = ndKillChar; /* dummy */
[73a9ffb]381  n->ch = 0;
[51d835]382
383  n->cfDelete  = ngcDelete;
[7bbbef]384  n->cfNormalize=ndNormalize;
[51d835]385  n->cfInit   = ngcInit;
[7bbbef]386  n->cfInt    = ngcInt;
387  n->cfAdd     = ngcAdd;
388  n->cfSub     = ngcSub;
389  n->cfMult    = ngcMult;
390  n->cfDiv     = ngcDiv;
391  n->cfExactDiv= ngcDiv;
392  n->cfNeg     = ngcNeg;
393  n->cfInvers  = ngcInvers;
[51d835]394  n->cfCopy   = ngcCopy;
[7bbbef]395  n->cfGreater = ngcGreater;
396  n->cfEqual   = ngcEqual;
397  n->cfIsZero  = ngcIsZero;
398  n->cfIsOne   = ngcIsOne;
399  n->cfIsMOne  = ngcIsMOne;
400  n->cfGreaterZero = ngcGreaterZero;
[51d835]401  n->cfWrite  = ngcWrite;
[7bbbef]402  n->cfRead    = ngcRead;
403  n->cfPower   = ngcPower;
[51d835]404  n->cfSetMap = ngcSetMap;
[7bbbef]405  n->cfRePart  = ngcRePart;
406  n->cfImPart  = ngcImPart;
[7a8011]407  n->cfCoeffWrite = ngcCoeffWrite;
[7bbbef]408    // cfSize  = ndSize;
[51d835]409#ifdef LDEBUG
[7bbbef]410  n->cfDBTest  = ndDBTest; // not yet implemented: ngcDBTest
[51d835]411#endif
412
[121fd9]413  n->nCoeffIsEqual = ndCoeffIsEqual;
[039016]414
[51d835]415
[9bb5457]416
417/*
[51d835]418  //r->cfInitChar=nlInitChar;
419  r->cfKillChar=NULL;
420  r->cfSetChar=NULL;
421  r->nCoeffIsEqual=nlCoeffsEqual;
422
[7bbbef]423  r->cfMult  = nlMult;
424  r->cfSub   = nlSub;
425  r->cfAdd   = nlAdd;
426  r->cfDiv   = nlDiv;
427  r->cfIntDiv= nlIntDiv;
428  r->cfIntMod= nlIntMod;
429  r->cfExactDiv= nlExactDiv;
[51d835]430  r->cfInit = nlInit;
[7bbbef]431  r->cfSize  = nlSize;
432  r->cfInt  = nlInt;
[51d835]433#ifdef HAVE_RINGS
[7bbbef]434  r->cfDivComp = NULL; // only for ring stuff
435  r->cfIsUnit = NULL; // only for ring stuff
436  r->cfGetUnit = NULL; // only for ring stuff
437  r->cfExtGcd = NULL; // only for ring stuff
[51d835]438#endif
[7bbbef]439  r->cfNeg   = nlNeg;
440  r->cfInvers= nlInvers;
[51d835]441  r->cfCopy  = nl_Copy;
[7bbbef]442  r->cfRePart = nl_Copy;
443  r->cfImPart = ndReturn0;
[51d835]444  r->cfWrite = nlWrite;
[7bbbef]445  r->cfRead = nlRead;
446  r->cfNormalize=nlNormalize;
447  r->cfGreater = nlGreater;
[51d835]448#ifdef HAVE_RINGS
[7bbbef]449  r->cfDivBy = NULL; // only for ring stuff
[51d835]450#endif
[7bbbef]451  r->cfEqual = nlEqual;
452  r->cfIsZero = nlIsZero;
453  r->cfIsOne = nlIsOne;
454  r->cfIsMOne = nlIsMOne;
455  r->cfGreaterZero = nlGreaterZero;
456  r->cfPower = nlPower;
[51d835]457  r->cfGetDenom = nlGetDenom;
458  r->cfGetNumerator = nlGetNumerator;
[7bbbef]459  r->cfGcd  = nlGcd;
460  r->cfLcm  = nlLcm;
[51d835]461  r->cfDelete= nlDelete;
462  r->cfSetMap = nlSetMap;
[7bbbef]463  r->cfName = ndName;
464  r->cfInpMult=nlInpMult;
465  r->cfInit_bigint=nlCopyMap;
[51d835]466#ifdef LDEBUG
467  // debug stuff
[7bbbef]468  r->cfDBTest=nlDBTest;
[51d835]469#endif
470
471  // the variables:
472  r->nNULL = INT_TO_SR(0);
473  r->type = n_Q;
474  r->ch = 0;
475  r->has_simple_Alloc=FALSE;
476  r->has_simple_Inverse=FALSE;
477*/
478
479/// TODO: Any variables?
[4c6e420]480  if( p == NULL )
[121fd9]481    n->complex_parameter = omStrDup((char*)"i");
[4c6e420]482  else
[bfa04e]483    n->complex_parameter = omStrDup( (char*) p );
[9bb5457]484
[5d594a9]485  return FALSE;
[51d835]486}
487
488
489
490
491
492number ngcMapQ(number from, const coeffs aRing, const coeffs r)
493{
494  assume( getCoeffType(r) == ID );
495  assume( getCoeffType(aRing) == n_Q );
496
497  if ( from != NULL )
498  {
499    gmp_complex *res=new gmp_complex(numberFieldToFloat(from,QTOF,aRing));
500    return (number)res;
501  }
502  else
503    return NULL;
504}
505
506static number ngcMapLongR(number from, const coeffs aRing, const coeffs r)
507{
508  assume( getCoeffType(r) == ID );
509  assume( getCoeffType(aRing) == n_long_R );
510
511  if ( from != NULL )
512  {
513    gmp_complex *res=new gmp_complex(*((gmp_float *)from));
514    return (number)res;
515  }
516  else
517    return NULL;
518}
519
520static number ngcMapR(number from, const coeffs aRing, const coeffs r)
521{
522  assume( getCoeffType(r) == ID );
523  assume( getCoeffType(aRing) == n_R );
524
525  if ( from != NULL )
526  {
527    gmp_complex *res=new gmp_complex((double)nrFloat(from));
528    return (number)res;
529  }
530  else
531    return NULL;
532}
533
534static number ngcMapP(number from, const coeffs aRing, const coeffs r)
535{
536  assume( getCoeffType(r) == ID );
537  assume( getCoeffType(aRing) ==  n_Zp );
538
[210852]539  if ( from != NULL )
[51d835]540    return ngcInit(npInt(from, aRing), r);
541  else
542    return NULL;
543}
544
545static number ngcCopyMap(number from, const coeffs aRing, const coeffs r)
546{
547  assume( getCoeffType(r) == ID );
548  assume( getCoeffType(aRing) ==  ID );
549
550  gmp_complex* b = NULL;
551
552  if ( from !=  NULL )
[9bb5457]553  {
[51d835]554    b = new gmp_complex( *(gmp_complex*)from );
555  }
[9bb5457]556  return (number)b;
[51d835]557}
558
559nMapFunc ngcSetMap(const coeffs src, const coeffs dst)
560{
561  assume( getCoeffType(dst) == ID );
562
[1cce47]563  if (nCoeff_is_Q(src))
[51d835]564  {
565    return ngcMapQ;
566  }
[1cce47]567  if (nCoeff_is_long_R(src))
[51d835]568  {
569    return ngcMapLongR;
570  }
[1cce47]571  if (nCoeff_is_long_C(src))
[51d835]572  {
573    return ngcCopyMap;
574  }
[1cce47]575  if (nCoeff_is_R(src))
[51d835]576  {
577    return ngcMapR;
578  }
[1cce47]579  if (nCoeff_is_Zp(src))
[51d835]580  {
581    return ngcMapP;
582  }
583  return NULL;
584}
585
[7a8011]586void    ngcCoeffWrite  (const coeffs r)
587{
588  Print("//   characteristic : 0 (complex:%d digits, additional %d digits)\n",
589               r->float_len, r->float_len2);  /* long C */
590}
Note: See TracBrowser for help on using the repository browser.