source: git/Singular/eigenval_ip.cc @ 76b4bd

spielwiese
Last change on this file since 76b4bd was 76b4bd, checked in by Hans Schönemann <hannes@…>, 20 years ago
*hannes: eigenval->eigenval_ip, kernel/eigenval git-svn-id: file:///usr/local/Singular/svn/trunk@7073 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 5.9 KB
Line 
1/*****************************************
2*  Computer Algebra System SINGULAR      *
3*****************************************/
4/* $Id: eigenval_ip.cc,v 1.1 2004-03-04 10:06:50 Singular Exp $ */
5/*
6* ABSTRACT: eigenvalues of constant square matrices
7*/
8
9#include "mod2.h"
10
11#ifdef HAVE_EIGENVAL
12
13#include "febase.h"
14#include "tok.h"
15#include "ipid.h"
16#include "intvec.h"
17#include "numbers.h"
18#include "polys.h"
19#include "ideals.h"
20#include "lists.h"
21#include "matpol.h"
22#include "clapsing.h"
23#include "eigenval.h"
24#include "eigenval_ip.h"
25
26
27BOOLEAN evSwap(leftv res,leftv h)
28{
29  if(currRingHdl)
30  {
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      {
37        int i=(int)h->Data();
38        h=h->next;
39        if(h&&h->Typ()==INT_CMD)
40        {
41          int j=(int)h->Data();
42          res->rtyp=MATRIX_CMD;
43          res->data=(void *)evSwap(mpCopy(M),i,j);
44          return FALSE;
45        }
46      }
47    }
48    WerrorS("<matrix>,<int>,<int> expected");
49    return TRUE;
50  }
51  WerrorS("no ring active");
52  return TRUE;
53}
54
55BOOLEAN evRowElim(leftv res,leftv h)
56{
57  if(currRingHdl)
58  {
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      {
65        int i=(int)h->Data();
66        h=h->next;
67        if(h&&h->Typ()==INT_CMD)
68        {
69          int j=(int)h->Data();
70          h=h->next;
71          if(h&&h->Typ()==INT_CMD)
72          {
73            int k=(int)h->Data();
74            res->rtyp=MATRIX_CMD;
75            res->data=(void *)evRowElim(mpCopy(M),i,j,k);
76            return FALSE;
77          }
78        }
79      }
80    }
81    WerrorS("<matrix>,<int>,<int>,<int> expected");
82    return TRUE;
83  }
84  WerrorS("no ring active");
85  return TRUE;
86}
87
88BOOLEAN evColElim(leftv res,leftv h)
89{
90  if(currRingHdl)
91  {
92    if(h&&h->Typ()==MATRIX_CMD)
93    {
94      matrix M=(matrix)h->Data();
95      h=h->next;
96      if(h&&h->Typ()==INT_CMD)
97      {
98        int i=(int)h->Data();
99        h=h->next;
100        if(h&&h->Typ()==INT_CMD)
101        {
102          int j=(int)h->Data();
103          h=h->next;
104          if(h&&h->Typ()==INT_CMD)
105          {
106            int k=(int)h->Data();
107            res->rtyp=MATRIX_CMD;
108            res->data=(void *)evColElim(mpCopy(M),i,j,k);
109            return FALSE;
110          }
111        }
112      }
113    }
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)
122{
123  if(currRingHdl)
124  {
125    if(h&&h->Typ()==MATRIX_CMD)
126    {
127      matrix M=(matrix)h->Data();
128      res->rtyp=MATRIX_CMD;
129      res->data=(void *)evHessenberg(mpCopy(M));
130      return FALSE;
131    }
132    WerrorS("<matrix> expected");
133    return TRUE;
134  }
135  WerrorS("no ring active");
136  return TRUE;
137}
138
139
140lists evEigenvals(matrix M)
141{
142  lists l=(lists)omAllocBin(slists_bin);
143  if(MATROWS(M)!=MATCOLS(M))
144  {
145    l->Init(0);
146    return(l);
147  }
148
149  M=evHessenberg((matrix)idJet((ideal)M,0));
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);
157  pSetm(t);
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)
162      j++;
163    if(j==j0+1)
164    {
165      e->m[k]=pHead(MATELEM(M,j0,j0));
166      (*m)[k]=1;
167      k++;
168    }
169    else
170    {
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;
182      ideal e0=singclap_factorize(mpDetBareiss(M0),&m0,2);
183
184      for(int i=0;i<IDELEMS(e0);i++)
185      {
186        if(pNext(e0->m[i])==NULL)
187        {
188          (*m)[k]=(*m0)[i];
189          k++;
190        }
191        else
192        if(pGetExp(e0->m[i],1)<2&&pGetExp(pNext(e0->m[i]),1)<2&&
193           pNext(pNext(e0->m[i]))==NULL)
194        {
195          number e1=nNeg(nCopy(pGetCoeff(e0->m[i])));
196          if(pGetExp(pNext(e0->m[i]),1)==0)
197            e->m[k]=pNSet(nDiv(pGetCoeff(pNext(e0->m[i])),e1));
198          else
199            e->m[k]=pNSet(nDiv(e1,pGetCoeff(pNext(e0->m[i]))));
200          nDelete(&e1);
201          pNormalize(e->m[k]);
202          (*m)[k]=(*m0)[i];
203          k++;
204        }
205        else
206        {
207          e->m[k]=e0->m[i];
208          pNormalize(e->m[k]);
209          e0->m[i]=NULL;
210          (*m)[k]=(*m0)[i];
211          k++;
212        }
213      }
214
215      delete(m0);
216      idDelete(&e0);
217    }
218  }
219
220  pDelete(&t);
221  idDelete((ideal *)&M);
222
223  for(int i0=0;i0<n-1;i0++)
224  {
225    for(int i1=i0+1;i1<n;i1++)
226    {
227      if(pEqualPolys(e->m[i0],e->m[i1]))
228      {
229        (*m)[i0]+=(*m)[i1];
230        (*m)[i1]=0;
231      }
232      else
233      {
234        if(e->m[i0]==NULL&&!nGreaterZero(pGetCoeff(e->m[i1]))||
235           e->m[i1]==NULL&&
236          (nGreaterZero(pGetCoeff(e->m[i0]))||pNext(e->m[i0])!=NULL)||
237           e->m[i0]!=NULL&&e->m[i1]!=NULL&&
238          (pNext(e->m[i0])!=NULL&&pNext(e->m[i1])==NULL||
239           pNext(e->m[i0])==NULL&&pNext(e->m[i1])==NULL&&
240           nGreater(pGetCoeff(e->m[i0]),pGetCoeff(e->m[i1]))))
241        {
242          poly e1=e->m[i0];
243          e->m[i0]=e->m[i1];
244          e->m[i1]=e1;
245          int m1=(*m)[i0];
246          (*m)[i0]=(*m)[i1];
247          (*m)[i1]=m1;
248        }
249      }
250    }
251  }
252
253  int n0=0;
254  for(int i=0;i<n;i++)
255    if((*m)[i]>0)
256      n0++;
257
258  ideal e0=idInit(n0,1);
259  intvec *m0=new intvec(n0);
260
261  for(int i=0,i0=0;i<n;i++)
262    if((*m)[i]>0)
263    {
264      e0->m[i0]=e->m[i];
265      e->m[i]=NULL;
266      (*m0)[i0]=(*m)[i];
267      i0++;
268    }
269
270  idDelete(&e);
271  delete(m);
272
273  l->Init(2);
274  l->m[0].rtyp=IDEAL_CMD;
275  l->m[0].data=e0;
276  l->m[1].rtyp=INTVEC_CMD;
277  l->m[1].data=m0;
278
279  return(l);
280}
281
282
283BOOLEAN evEigenvals(leftv res,leftv h)
284{
285  if(currRingHdl)
286  {
287    if(h&&h->Typ()==MATRIX_CMD)
288    {
289      matrix M=(matrix)h->Data();
290      res->rtyp=LIST_CMD;
291      res->data=(void *)evEigenvals(mpCopy(M));
292      return FALSE;
293    }
294    WerrorS("<matrix> expected");
295    return TRUE;
296  }
297  WerrorS("no ring active");
298  return TRUE;
299}
300
301#endif /* HAVE_EIGENVAL */
Note: See TracBrowser for help on using the repository browser.