source: git/Singular/eigenval_ip.cc @ f7ed7d

fieker-DuValspielwiese
Last change on this file since f7ed7d was 6ce030f, checked in by Oleksandr Motsak <motsak@…>, 12 years ago
removal of the $Id$ svn tag from everywhere NOTE: the git SHA1 may be used instead (only on special places) NOTE: the libraries Singular/LIB/*.lib still contain the marker due to our current use of svn
  • Property mode set to 100644
File size: 6.3 KB
RevLine 
[a7f2a04]1/*****************************************
2*  Computer Algebra System SINGULAR      *
3*****************************************/
[e4743bc]4/*
5* ABSTRACT: eigenvalues of constant square matrices
6*/
7
[762407]8#include "config.h"
[b1dfaf]9#include <kernel/mod2.h>
[e4743bc]10
11#ifdef HAVE_EIGENVAL
12
[599326]13#include <kernel/febase.h>
14#include <Singular/tok.h>
15#include <Singular/ipid.h>
[0fb34ba]16#include <misc/intvec.h>
17#include <coeffs/numbers.h>
[737a68]18#include <kernel/polys.h>
[599326]19#include <kernel/ideals.h>
20#include <Singular/lists.h>
[0fb34ba]21#include <polys/matpol.h>
[47417b]22#include <polys/clapsing.h>
[9326694]23#include <kernel/eigenval.h>
[599326]24#include <Singular/eigenval_ip.h>
[e4743bc]25
[a7f2a04]26
27BOOLEAN evSwap(leftv res,leftv h)
[e4743bc]28{
[7d58b6]29  if(currRing)
[e4743bc]30  {
[a7f2a04]31    if(h&&h->Typ()==MATRIX_CMD)
32    {
33      matrix M=(matrix)h->Data();
34      h=h->next;
35      if(h&&h->Typ()==INT_CMD)
36      {
[6caadfb]37        int i=(int)(long)h->Data();
[a7f2a04]38        h=h->next;
39        if(h&&h->Typ()==INT_CMD)
40        {
[6caadfb]41          int j=(int)(long)h->Data();
[a7f2a04]42          res->rtyp=MATRIX_CMD;
[8ea869]43          res->data=(void *)evSwap(mp_Copy(M, currRing),i,j);
[a7f2a04]44          return FALSE;
45        }
46      }
47    }
48    WerrorS("<matrix>,<int>,<int> expected");
49    return TRUE;
[e4743bc]50  }
[a7f2a04]51  WerrorS("no ring active");
52  return TRUE;
53}
54
55BOOLEAN evRowElim(leftv res,leftv h)
56{
[7d58b6]57  if(currRing)
[e4743bc]58  {
[a7f2a04]59    if(h&&h->Typ()==MATRIX_CMD)
60    {
61      matrix M=(matrix)h->Data();
62      h=h->next;
63      if(h&&h->Typ()==INT_CMD)
64      {
[6caadfb]65        int i=(int)(long)h->Data();
[a7f2a04]66        h=h->next;
67        if(h&&h->Typ()==INT_CMD)
68        {
[6caadfb]69          int j=(int)(long)h->Data();
[a7f2a04]70          h=h->next;
71          if(h&&h->Typ()==INT_CMD)
72          {
[6caadfb]73            int k=(int)(long)h->Data();
[a7f2a04]74            res->rtyp=MATRIX_CMD;
[8ea869]75            res->data=(void *)evRowElim(mp_Copy(M, currRing),i,j,k);
[a7f2a04]76            return FALSE;
77          }
78        }
79      }
80    }
81    WerrorS("<matrix>,<int>,<int>,<int> expected");
82    return TRUE;
[e4743bc]83  }
[a7f2a04]84  WerrorS("no ring active");
85  return TRUE;
[e4743bc]86}
87
[a7f2a04]88BOOLEAN evColElim(leftv res,leftv h)
[e4743bc]89{
[7d58b6]90  if(currRing)
[e4743bc]91  {
[a7f2a04]92    if(h&&h->Typ()==MATRIX_CMD)
[e4743bc]93    {
[a7f2a04]94      matrix M=(matrix)h->Data();
95      h=h->next;
96      if(h&&h->Typ()==INT_CMD)
[e4743bc]97      {
[6caadfb]98        int i=(int)(long)h->Data();
[a7f2a04]99        h=h->next;
100        if(h&&h->Typ()==INT_CMD)
101        {
[6caadfb]102          int j=(int)(long)h->Data();
[a7f2a04]103          h=h->next;
104          if(h&&h->Typ()==INT_CMD)
105          {
[6caadfb]106            int k=(int)(long)h->Data();
[a7f2a04]107            res->rtyp=MATRIX_CMD;
[8ea869]108            res->data=(void *)evColElim(mp_Copy(M, currRing),i,j,k);
[a7f2a04]109            return FALSE;
110          }
111        }
[e4743bc]112      }
113    }
[a7f2a04]114    WerrorS("<matrix>,<int>,<int>,<int> expected");
115    return TRUE;
116  }
117  WerrorS("no ring active");
118  return TRUE;
119}
120
121BOOLEAN evHessenberg(leftv res,leftv h)
[e4743bc]122{
[7d58b6]123  if(currRing)
[e4743bc]124  {
[a7f2a04]125    if(h&&h->Typ()==MATRIX_CMD)
[e4743bc]126    {
[a7f2a04]127      matrix M=(matrix)h->Data();
128      res->rtyp=MATRIX_CMD;
[8ea869]129      res->data=(void *)evHessenberg(mp_Copy(M, currRing));
[a7f2a04]130      return FALSE;
[e4743bc]131    }
[a7f2a04]132    WerrorS("<matrix> expected");
133    return TRUE;
[e4743bc]134  }
[a7f2a04]135  WerrorS("no ring active");
136  return TRUE;
[e4743bc]137}
138
139
[7656d7]140lists evEigenvals(matrix M)
[e4743bc]141{
142  lists l=(lists)omAllocBin(slists_bin);
[a7f2a04]143  if(MATROWS(M)!=MATCOLS(M))
[e4743bc]144  {
[a7f2a04]145    l->Init(0);
146    return(l);
147  }
148
[e511e0]149  M=evHessenberg((matrix)id_Jet((ideal)M,0,currRing));
[a7f2a04]150
151  int n=MATROWS(M);
152  ideal e=idInit(n,1);
153  intvec *m=new intvec(n);
154
155  poly t=pOne();
156  pSetExp(t,1,1);
[dcff1e]157  pSetm(t);
[a7f2a04]158
159  for(int j0=1,j=2,k=0;j<=n+1;j0=j,j++)
160  {
161    while(j<=n&&MATELEM(M,j,j-1)!=NULL)
[e4743bc]162      j++;
[a7f2a04]163    if(j==j0+1)
164    {
165      e->m[k]=pHead(MATELEM(M,j0,j0));
166      (*m)[k]=1;
167      k++;
[e4743bc]168    }
[a7f2a04]169    else
[e4743bc]170    {
[a7f2a04]171      int n0=j-j0;
172      matrix M0=mpNew(n0,n0);
173
174      j0--;
175      for(int i=1;i<=n0;i++)
176        for(int j=1;j<=n0;j++)
177          MATELEM(M0,i,j)=pCopy(MATELEM(M,j0+i,j0+j));
178      for(int i=1;i<=n0;i++)
179        MATELEM(M0,i,i)=pSub(MATELEM(M0,i,i),pCopy(t));
180
181      intvec *m0;
[fea2af]182#ifdef HAVE_FACTORY     
[8ea869]183      ideal e0=singclap_factorize(mp_DetBareiss(M,currRing),&m0,2, currRing);
[fea2af]184#else
185      WarnS("cannot factorize due to missing module 'factory'");
186      ideal e0=NULL;
187#endif
188     
[ace7cca]189      if (e0==NULL)
190      {
191        l->Init(0);
192        return(l);
193      }
[a7f2a04]194
195      for(int i=0;i<IDELEMS(e0);i++)
[e4743bc]196      {
[f7920d7]197        if(pNext(e0->m[i])==NULL)
198        {
199          (*m)[k]=(*m0)[i];
200          k++;
201        }
202        else
203        if(pGetExp(e0->m[i],1)<2&&pGetExp(pNext(e0->m[i]),1)<2&&
204           pNext(pNext(e0->m[i]))==NULL)
205        {
[3972adb]206          number e1=nNeg(nCopy(pGetCoeff(e0->m[i])));
[f7920d7]207          if(pGetExp(pNext(e0->m[i]),1)==0)
208            e->m[k]=pNSet(nDiv(pGetCoeff(pNext(e0->m[i])),e1));
209          else
210            e->m[k]=pNSet(nDiv(e1,pGetCoeff(pNext(e0->m[i]))));
211          nDelete(&e1);
[15d67db]212          pNormalize(e->m[k]);
[f7920d7]213          (*m)[k]=(*m0)[i];
214          k++;
215        }
[e4743bc]216        else
[f7920d7]217        {
218          e->m[k]=e0->m[i];
[15d67db]219          pNormalize(e->m[k]);
[f7920d7]220          e0->m[i]=NULL;
221          (*m)[k]=(*m0)[i];
222          k++;
223        }
[e4743bc]224      }
[a7f2a04]225
226      delete(m0);
[e4743bc]227      idDelete(&e0);
228    }
229  }
230
[a7f2a04]231  pDelete(&t);
232  idDelete((ideal *)&M);
233
[f7920d7]234  for(int i0=0;i0<n-1;i0++)
[e4743bc]235  {
[f7920d7]236    for(int i1=i0+1;i1<n;i1++)
[e4743bc]237    {
[f7920d7]238      if(pEqualPolys(e->m[i0],e->m[i1]))
[a7f2a04]239      {
[f7920d7]240        (*m)[i0]+=(*m)[i1];
241        (*m)[i1]=0;
[a7f2a04]242      }
243      else
244      {
[f7920d7]245        if(e->m[i0]==NULL&&!nGreaterZero(pGetCoeff(e->m[i1]))||
246           e->m[i1]==NULL&&
247          (nGreaterZero(pGetCoeff(e->m[i0]))||pNext(e->m[i0])!=NULL)||
248           e->m[i0]!=NULL&&e->m[i1]!=NULL&&
249          (pNext(e->m[i0])!=NULL&&pNext(e->m[i1])==NULL||
250           pNext(e->m[i0])==NULL&&pNext(e->m[i1])==NULL&&
251           nGreater(pGetCoeff(e->m[i0]),pGetCoeff(e->m[i1]))))
252        {
253          poly e1=e->m[i0];
254          e->m[i0]=e->m[i1];
255          e->m[i1]=e1;
256          int m1=(*m)[i0];
257          (*m)[i0]=(*m)[i1];
258          (*m)[i1]=m1;
259        }
[a7f2a04]260      }
[e4743bc]261    }
262  }
[a7f2a04]263
264  int n0=0;
265  for(int i=0;i<n;i++)
266    if((*m)[i]>0)
267      n0++;
268
269  ideal e0=idInit(n0,1);
270  intvec *m0=new intvec(n0);
271
272  for(int i=0,i0=0;i<n;i++)
273    if((*m)[i]>0)
274    {
275      e0->m[i0]=e->m[i];
276      e->m[i]=NULL;
277      (*m0)[i0]=(*m)[i];
278      i0++;
279    }
280
281  idDelete(&e);
282  delete(m);
283
284  l->Init(2);
285  l->m[0].rtyp=IDEAL_CMD;
286  l->m[0].data=e0;
287  l->m[1].rtyp=INTVEC_CMD;
288  l->m[1].data=m0;
289
290  return(l);
[e4743bc]291}
292
[a7f2a04]293
[7656d7]294BOOLEAN evEigenvals(leftv res,leftv h)
[e4743bc]295{
[7d58b6]296  if(currRing)
[e4743bc]297  {
[a7f2a04]298    if(h&&h->Typ()==MATRIX_CMD)
[e4743bc]299    {
[a7f2a04]300      matrix M=(matrix)h->Data();
301      res->rtyp=LIST_CMD;
[8ea869]302      res->data=(void *)evEigenvals(mp_Copy(M, currRing));
[a7f2a04]303      return FALSE;
[5589c0]304    }
[a7f2a04]305    WerrorS("<matrix> expected");
306    return TRUE;
[e4743bc]307  }
[a7f2a04]308  WerrorS("no ring active");
[4fc824e]309  return TRUE;
[e4743bc]310}
[a7f2a04]311
312#endif /* HAVE_EIGENVAL */
Note: See TracBrowser for help on using the repository browser.