Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #61593 > unrolled thread

Column slices

Started byluser droog <luser.droog@gmail.com>
First post2015-05-05 23:18 -0700
Last post2015-05-25 00:13 -0700
Articles 10 on this page of 50 — 10 participants

Back to article view | Back to comp.lang.c


Contents

  Column slices luser droog <luser.droog@gmail.com> - 2015-05-05 23:18 -0700
    Column slices asetofsymbols@gmail.com - 2015-05-06 00:26 -0700
      Re: Column slices luser droog <luser.droog@gmail.com> - 2015-05-06 02:33 -0700
        Re: Column slices Bartc <bc@freeuk.com> - 2015-05-06 10:46 +0100
        Re: Column slices asetofsymbols@gmail.com - 2015-05-07 11:02 -0700
          Re: Column slices asetofsymbols@gmail.com - 2015-05-07 14:05 -0700
    Re: Column slices Nobody <nobody@nowhere.invalid> - 2015-05-06 08:29 +0100
    Re: Column slices Bartc <bc@freeuk.com> - 2015-05-06 10:30 +0100
      Re: Column slices luser droog <luser.droog@gmail.com> - 2015-05-06 03:11 -0700
        Re: Column slices asetofsymbols@gmail.com - 2015-05-08 09:05 -0700
    Re: Column slices luser droog <luser.droog@gmail.com> - 2015-05-11 16:50 -0700
      Re: Column slices asetofsymbols@gmail.com - 2015-05-11 17:24 -0700
        Re: Column slices asetofsymbols@gmail.com - 2015-05-11 19:01 -0700
          Re: Column slices luser droog <luser.droog@gmail.com> - 2015-05-19 09:56 -0700
            Re: Column slices asetofsymbols@gmail.com - 2015-05-19 10:43 -0700
              Re: Column slices asetofsymbols@gmail.com - 2015-05-19 11:16 -0700
                Re: Column slices asetofsymbols@gmail.com - 2015-05-19 11:27 -0700
                Re: Column slices luser droog <luser.droog@gmail.com> - 2015-05-19 21:28 -0700
                  Re: Column slices asetofsymbols@gmail.com - 2015-05-20 08:05 -0700
              Re: Column slices luser droog <luser.droog@gmail.com> - 2015-05-19 21:20 -0700
                Re: Column slices asetofsymbols@gmail.com - 2015-05-20 08:55 -0700
                  Re: Column slices asetofsymbols@gmail.com - 2015-05-20 08:58 -0700
                    Re: Column slices asetofsymbols@gmail.com - 2015-05-21 01:22 -0700
                      Re: Column slices asetofsymbols@gmail.com - 2015-05-21 01:34 -0700
                      Re: Column slices asetofsymbols@gmail.com - 2015-05-26 19:17 -0700
                      Re: Column slices asetofsymbols@gmail.com - 2015-05-31 13:56 -0700
                        Re: Column slices Keith Thompson <kst-u@mib.org> - 2015-05-31 14:23 -0700
                          Re: Column slices Ian Collins <ian-news@hotmail.com> - 2015-06-01 09:37 +1200
                      Re: Column slices asetofsymbols@gmail.com - 2015-06-01 01:23 -0700
                        Re: Column slices Ian Collins <ian-news@hotmail.com> - 2015-06-01 20:35 +1200
                          Re: Column slices mark.bluemel@gmail.com - 2015-06-01 03:50 -0700
                          Re: Column slices luser droog <luser.droog@gmail.com> - 2015-06-01 18:04 -0700
                            Re: Column slices luser droog <luser.droog@gmail.com> - 2015-06-01 18:16 -0700
                            Re: Column slices "Chris M. Thomasson" <nospam@nospam.nospam> - 2015-06-01 19:57 -0700
                            Re: Column slices Rosario19 <Ros@invalid.invalid> - 2015-06-02 08:30 +0200
                              Re: Column slices luser droog <luser.droog@gmail.com> - 2015-06-02 00:22 -0700
      Re: Column slices Ike Naar <ike@iceland.freeshell.org> - 2015-05-12 06:06 +0000
        Re: Column slices Ike Naar <ike@iceland.freeshell.org> - 2015-05-12 21:15 +0000
      Re: Column slices asetofsymbols@gmail.com - 2015-05-13 20:49 -0700
        Re: Column slices luser droog <luser.droog@gmail.com> - 2015-05-13 23:43 -0700
          Re: Column slices luser droog <luser.droog@gmail.com> - 2015-05-13 23:44 -0700
          Re: Column slices asetofsymbols@gmail.com - 2015-05-14 01:02 -0700
            Re: Column slices asetofsymbols@gmail.com - 2015-05-15 23:06 -0700
              Re: Column slices asetofsymbols@gmail.com - 2015-05-16 03:46 -0700
    Re: Column slices luser droog <luser.droog@gmail.com> - 2015-05-23 00:17 -0700
      Re: Column slices asetofsymbols@gmail.com - 2015-05-23 07:59 -0700
        Re: Column slices asetofsymbols@gmail.com - 2015-05-23 21:25 -0700
          Re: Column slices luser droog <luser.droog@gmail.com> - 2015-05-24 18:29 -0700
        Re: Column slices luser droog <luser.droog@gmail.com> - 2015-05-24 18:08 -0700
          Re: Column slices asetofsymbols@gmail.com - 2015-05-25 00:13 -0700

Page 3 of 3 — ← Prev page 1 2 [3]


#61972

Fromluser droog <luser.droog@gmail.com>
Date2015-05-13 23:44 -0700
Message-ID<f7cf11dc-6764-4748-ae7f-71840adf382b@googlegroups.com>
In reply to#61971
On Thursday, May 14, 2015 at 1:43:12 AM UTC-5, luser droog wrote:
> On Wednesday, May 13, 2015 at 10:49:35 PM UTC-5, asetof...@gmail.com wrote:
> > But use of complex macros
> >  with argument can be not good 
> > the time one has to debug...
> > 
> > The max complexity in macros 
> >  I have to debug
> > it is the macro
> > #define G(a, b) if(a)goto b
> > (the only macro with argument
> > I remember I use)
> > that not show problems in debug.
> > I have to say the compiler traslate
> > that in an easy neat way
> 
> At the risk of having some of my own code
> shoved in my face, I daresay that G macro
> makes things just about impossible to read.
> Perhaps with the aid of debugger, but, ...
> 
> I have tried to read your code posts to
> try to get more ideas, but it's only a
> slight exaggeration to say that all I 
> could understand was "..." (which I 
> promptly borrowed, so thank you).
> 
> I think your G macro is precisely the sort
> of jumping that Dijkstra was talking about.
> Yes, it's tremendously powerful! Yes, it
> can simulate any other control structure 
> you could possibly want, including the 
> basic suite. But ... It makes everything
> look the same. It's easier to visually 
> distinguish "if" from "while" than 
> "G(??)...G(???)...G(??)" from "G(?)...G(?)".

belated pithy punchline:

That's what the compiler is for!
If you want machine code you know where to find it.

-- 
sure, if they put the melody back in...

[toc] | [prev] | [next] | [standalone]


#61976

Fromasetofsymbols@gmail.com
Date2015-05-14 01:02 -0700
Message-ID<a67f4d67-5eed-45c0-9d67-874c6d6cc890@googlegroups.com>
In reply to#61971
Using indentation I not see all the
same

For example loop:
l0: Ist1; istr2; G(cond,l0);

Goto used for condition error
G(condErr, err); G(condErr1, err);


Goto used as if()
    G(!cond, l0); istr; istr;
l0:

[toc] | [prev] | [next] | [standalone]


#62094

Fromasetofsymbols@gmail.com
Date2015-05-15 23:06 -0700
Message-ID<77c61c63-d09d-4a26-b6d5-556e7a1ced2d@googlegroups.com>
In reply to#61976
On Thursday, May 14, 2015 at 10:02:18 AM UTC+2, asetof...@gmail.com wrote:
> Using indentation I not see all the
> same
> 
> For example loop:
> l0: Ist1; istr2; G(cond,l0);
> 
> Goto used for condition error
> G(condErr, err); G(condErr1, err);
> 
> 
> Goto used as if()
>     G(!cond, l0); istr; istr;
> l0:

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <iostream.h>

#define  G(a,b)   if(a)goto b
#define  R   return
#define  P   printf
#define  M   malloc
#define  Fr  free
#define  F   for
#define  S   sizeof
#define  u8  unsigned char
#define  u16 unsigned short
#define  u32 unsigned
#define  i32 int
#define  d64 double

u32  PBadAlloc; /* if some allocation error for all permutation here 1 */

template<class T>  class permutazioni{
public:

permutazioni(u32  a)
{u32   i;
 n=0; arr=0; c=0; cnt=0; ji=0;
 if(a==0||a>0xFF)  {PBadAlloc=1; R;}
 arr=(T*)malloc( a*sizeof(T) );
 if(arr==0){PBadAlloc=1; R;}
 c=(u32*)malloc( a*sizeof(u32) );
 if(c==0){free(arr); PBadAlloc=1; arr=0; R;}
 n=a;
 F(i=0; i<n; ++i){c[i]=0; arr[i]=i;}
}

~permutazioni(){free(c); free(arr);}

u32  swp(T*  a, u32 i, u32 j)
{T  x;
 if(i>=n||j>=n) R -1;
 if(i==j)       R  0;
 x=a[i]; a[i]=a[j]; a[j]=x; ++cnt;
 R  0;
}

/* from Sedgewick, result in arr [3!=6] */
u32  nextP(u32  a)
{u32          k;

 if(a==0)
   {F(k=0; k<n; ++k)  c[k]=0;
    ji=0; cnt=0;
    R  1;
   }
 F( ; ji<n; )
   {if(c[ji]<ji)
       {swp(arr, ((ji+1)%2? 0: c[ji]) , ji);
        ++c[ji]; ji=1;   // can be ok ji=0; too
        R  1;  //  ok
       }
    else {c[ji++]=0; }
   }
 R  0;  // for end
}

void  print(void)
{u32  i;
 F(i=0; i<n; ++i)
        if(i==0  ) cout<<"[ "<< arr[i]<<" ";
   else if(i==n-1) cout<<arr[i]<<" ]  ";
   else            cout<<arr[i]<<" ";
}

i32  e(void){R  PBadAlloc;}

T&  operator[](i32  i){R  arr[i];}

 u32     n;     /* number of element index 0..n-1 */
 T    *arr;     /* where they are */
 u32    *c;     /* index array for find next permutation*/
 u32    ji;     /* index for next permutation: nextP()  */
 u32   cnt;     /* cnt%2?-1:1 sign of permutation nextP() return*/
};

// functions for detect u32 or i32 overflow
u32  omul(u32  a,  u32  b)
{asm{mov eax, a
     mov edx, b
     mul edx
     cmp edx, 0
     je  lab
     mov eax, -1
lab:
 }
 R  _EAX;
}

u32  oadd(u32  a,  u32  b)
{asm{mov eax, a
     mov edx, b
     add eax, edx
     jnc  lab
     mov eax, -1
lab:
 }
 R  _EAX;
}

u32  osub(u32  a,  u32  b)
{asm{mov eax, a
     mov edx, b
     sub eax, edx
     jnc lab
     mov eax, -1
lab:
    }
 R  _EAX;
}

i32  omul(i32  a,  i32  b)
{asm{mov  eax, a
     mov  edx, b
     imul edx
     jno  lab
     mov  eax, 0x80000000
lab: 
    }
 R  _EAX;
}


i32  oadd(i32  a,  i32  b)
{asm{mov eax, a
     mov edx, b
     add eax, edx
     jno lab
     mov eax, 0x80000000
lab:
    }
 R  _EAX;
}

i32  osub(i32  a,  i32  b)
{asm{mov eax, a
     mov edx, b
     sub eax, edx
     jno lab
     mov eax, 0x80000000
lab: 
    }
 R  _EAX;
}

// 32 bit unsigned and signed with overflow
class ou32{
public:
ou32(){v=0;}
ou32 operator=( ou32 a){v=a.v; R *this;}
ou32 operator*=(ou32 a){v=omul(v, a.v); R *this;}
ou32 operator+=(ou32 a){v=oadd(v, a.v); R *this;}
ou32 operator-=(ou32 a){v=osub(v, a.v); R *this;}
ou32 operator/=(ou32 a){if(v==-1||a.v==-1) v=-1; 
                        else  v=v/a.v; 
                        R *this;
                       }
ou32 operator++(){v=(v==-1? -1: ++v); R  *this;}
ou32 operator++(int){ou32  t=*this; if(v!=-1) ++v; R  t;}
ou32 operator--(){v=(v==-1? -1: --v); R  *this;}
ou32 operator--(int){ou32  t=*this; if(v!=-1) --v; R  t;}
friend ou32 operator*(ou32  a, ou32  b){ou32  r; r.v=omul(a.v, b.v); R  r;}
friend ou32 operator+(ou32  a, ou32  b){ou32  r; r.v=oadd(a.v, b.v); R  r;}
friend ou32 operator-(ou32  a, ou32  b){ou32  r; r.v=osub(a.v, b.v); R  r;}
friend ou32 operator/(ou32  a, ou32  b)
{ou32  r; 
 if(a.v==-1||b.v==-1)  r.v=-1;
 else                  r.v=a.v/b.v;
 R  r;
}
friend ostream& operator<<(ostream&  ostr, ou32  a)
{if( a.v==-1 ) ostr<<"ErrElm"; else  ostr<<a.v; R  ostr;}
friend i32  operator==(ou32 a, ou32 b){R  a.v==b.v;}
friend i32  operator!=(ou32 a, ou32 b){R  a.v==b.v;}
friend i32  operator<=(ou32 a, ou32 b){R  a.v<=b.v;}
friend i32  operator>=(ou32 a, ou32 b){R  a.v>=b.v;}
friend i32  operator<( ou32 a, ou32 b){R  a.v< b.v;}
friend i32  operator>( ou32 a, ou32 b){R  a.v> b.v;}

u32  v;
};

class oi32{
public:
oi32(){v=0;}
operator i32() const{R  v;}
oi32  operator=(i32  h){v=h; R *this;}
oi32  operator=(oi32 a){v=a.v; R *this;}
oi32 operator*=(oi32 a){v=omul(v, a.v); R *this;}
oi32 operator+=(oi32 a){v=oadd(v, a.v); R *this;}
oi32 operator-=(oi32 a){v=osub(v, a.v); R *this;}
oi32 operator/=(oi32 a)
{if(v==0x80000000||a.v==0x80000000) v=0x80000000;
 else                               v=v/a.v;
 R   *this;
}
oi32 operator++(){v=(v==0x80000000? 0x80000000: ++v); R  *this;}
oi32 operator++(int){oi32  t=*this; if(v!=0x80000000) ++v; R  t;}
oi32 operator--(){v=(v==0x80000000? 0x80000000: --v); R  *this;}
oi32 operator--(int){oi32  t=*this; if(v!=0x80000000) --v; R  t;}
friend oi32 operator*(oi32  a, oi32  b){oi32  r; r.v=omul(a.v, b.v); R  r;}
friend oi32 operator+(oi32  a, oi32  b){oi32  r; r.v=oadd(a.v, b.v); R  r;}
friend oi32 operator-(oi32  a, oi32  b){oi32  r; r.v=osub(a.v, b.v); R  r;}
friend oi32 operator/(oi32  a, oi32  b)
{oi32  r;
 if(a.v==0x80000000||b.v==0x80000000)  r.v=0x80000000;
 else                                  r.v=a.v/b.v;
 R  r;
}
friend ostream& operator<<(ostream&  ostr, oi32  a)
{if( a.v==0x80000000 ) ostr<<"ErrElm"; else  ostr<<a.v; R  ostr;}
friend i32  operator==(oi32 a, oi32 b){R  a.v==b.v;}
friend i32  operator!=(oi32 a, oi32 b){R  a.v==b.v;}
friend i32  operator<=(oi32 a, oi32 b){R  a.v<=b.v;}
friend i32  operator>=(oi32 a, oi32 b){R  a.v>=b.v;}
friend i32  operator<( oi32 a, oi32 b){R  a.v< b.v;}
friend i32  operator>( oi32 a, oi32 b){R  a.v> b.v;}

i32  v;
};

/*
I[0],I[1],I[2],I[3],I[4],sizeElement, sz, led,  [matrix data]
0    1    2    3    4    5            6   7     8
1    2    3    4    5    6            7   8     9
               I[I[0]+1]=I[3]
                    I[I[0]+I[0]]=I[4]

I[0],I[1],..,I[p[0]], sizeElement, sz,      led,     [matrix data]
0    1       I[0]     2*I[0]+1     2*I[0]+2 2*I[0]+3 2*I[0]+4  indirizzo
1    2       I[0]+1   2*I[0]+2     2*I[0]+3 2*I[0]+4 2*I[0]+5  nelement
I[0]:         is the number of index as 32 bit
I[1]:         is the first max index as 32 bit
....
I[I[0]]:      is the last max index
I[I[0]+1]:    is w[0]
...
I[2*I[0]]:    is w[I[0]]
I[2*I[0]+1]:  is the size of element as 32 bit 5
I[2*I[0]+2]:  is sz size of   "data" area      6
I[2*I[0]+3]:  is led: 0 matrix is ok, 1 overflow or errors in matrix 7
sono 2*I[0]+4 elementi [8 elementi]
I[2*I[0]+4]:  is matrix "data" area            8
*/

// access to led as u32*
#define  Led(a)  ((a)+*(a)*2+3)
// access to size of matrix data    as u32*
#define  SZ(a)   ((a)+*(a)*2+2)
// access to size element of matrix as u32*
#define  SZe(a)  ((a)+*(a)*2+1)
// access to data                   as u8*
#define  Data(a) (u8*) ((a)+*(a)*2+4)

u32  MtrBadAlloc=0;

/*
  reserve mem space to matrix of element msize for dimension
  i0xi1x...xin taken as input
  where i0, i1 etc are max+1 index and n<=255
  the last max+1 index is -1 that end the input
  Return -1 for error
  Return  0 all ok
NB
 **The first function to call on pm [construct matrix] **
 **the last  function to call on pm [destruct  matrix] **
*/
i32 Mcon(u32**  pm, u32  msize, ...)
{va_list           ap;
 u32       v, i, s, j;
 u32         arr[256];

    G(pm, l1);
l0:     MtrBadAlloc=1; R -1;    // for error
n0:     va_end(ap); *pm=0; G(1,l0);
l1: G(msize, l3);Fr(*pm); *pm=0;/* free the Matrix */
l2:     R  0;                   // for all ok
y0:     va_end(ap); G(1,l2);
l3: va_start(ap, msize); G(msize>0xFFF, n0); G(msize==0, n0); i=0;
l4:    v=va_arg(ap, u32); //v the first index;if i==0 means no element
       G(v>0xFFFF && v!= -1, n0); G(v!=-1, la); G(i==0, n0);
           s=1; j=1;
l5:        s*=arr[j-1]; G(s>0xFFFFF, n0); ++j; G( j<=i, l5);
           s*=msize;    G(s>0xFFFFF, n0);
           *pm=(u32*)M((2*i+4)*sizeof(u32)+s); G(*pm==0, n0);
           (*pm)[0]=i; *SZe(*pm)=msize; *SZ(*pm)=s; *Led(*pm)=0;
           j=1; s=1;
l6:        (*pm)[j]=arr[j-1]; (*pm)[2*i+1-j]=s;
                        s*=arr[i-j]; ++j; G(j<=i, l6);
           G(1,y0);
la:    G(i>255, n0); arr[i++]=v; G(1,l4);
}

// return one pointer to element
// or 0 if find some error
/*
2x2             y*n  +x  (y,  x)e{0..m -1}x{0..n -1}
2   elm(i0,i1)=i0*M1+i1  (i0,i1)e{0..M0-1}x{0..M1-1}  Max=(M0-1)*M1+M1-1=M0*M1-1
se M0=7  M1=1    7x1   (1,1)
elm(i0,i1)=i0+i1  (i0,i1)e{0..6}x{0..0}  Max=(7-1)*1+1-1=6
se M0=1  M1=7    1x7   (7,1)
elm(i0,i1)=i0*7+i1   (i0,i1)e{0..0}x{0..6}  Max=(0)*7+7-1=6

3   elm(i0,i1,i2)=i0*M1*M2+i1*M1+i2  (i0,i1,i2)e{0..M0-1}x{0..M1-1}x{0..M2-1}
    Max=(M0-1)*M1*M2+(M1-1)*M2+(M2-1)=M0*M1*M2-M1*M2+M1*M2-M2+M2-1=M0*M1*M2-1
*/
u8*  Aelm(u32*  pm, ...)
{va_list              ap;
 u8                   *r;
 u32 maxn, size, v, s, i;

    G(pm,l1);
l0:   R  0;
n0:   va_end(ap); G(1,l0);
l1: maxn=pm[0]; size= *SZe(pm); G(size>0xFFFF, l0);
                              //                0 1
    G(maxn>=256||maxn==0,l0); //for 2x2 y*n+x  (y,x)e[0..m-1]x[0..n-1]
    va_start(ap, pm);         // gets all index       i0 i1  maxn=2
    i=1; s=0;                 /*  I[0]+1..2*I[0]  */
l3: v=va_arg(ap,u32); G(v>=pm[i],n0); s+=v*pm[maxn+i]; ++i; G(i<=maxn, l3);
    v=s*size; r=Data(pm); r+=v;
    va_end(ap);
    R r;
}

// for now only nxm matrix of u8 u16 u32  d64[double]
void PrntMtrx(u32*  m)
{va_list           ap;
 d64               dv;
 u8         *p, *pmax;
 u32    i,j,maxn, max, size, v;
 u32       Index[256];

    G(m, l1);
l0:     R;  // error
la: P("[Me]"); G(1,l0);
l1: G(m[0]>2, l0);
    size= *SZe(m);
    G(size>8||size==0, l0);
    G( *Led(m) , la);
    i=0; p=Data(m); pmax=p+(size*m[1]*m[2]);
m0: G(size==1,l2);G(size==2,l3);G(size==4,l4);G(size==8,l11);G(1,l0);
l11:dv=*(d64*)p;  P("%10.2f",dv); G(1,l5);
l2: v =*(u8*) p;  P("%3u ",   v); G(1,l5);
l3: v =*(u16*)p;  P("%5u ",   v); G(1,l5);
l4: v =*(u32*)p;  P("%10u ",  v);
l5: G( (i+1)%m[2]!=0, l6); P("\n");
l6: p+=size; ++i; G(p<pmax, m0);
    R;
}

template<class T> class Matrix{
public:
 Matrix(u32 a, u32  b){Mcon(&mtr, S(T), a, b, -1);}
 Matrix(u32 a, u32  b, u32  c){Mcon(&mtr, S(T), a, b, c, -1);}
 Matrix(u32 a, u32  b, u32  c, u32  d){Mcon(&mtr, S(T), a, b, c, d, -1);}
~Matrix(){Mcon(&mtr, 0);}

T&  e(u32 i, u32 j)        {R *(T*) Aelm(mtr, i,j);}
T&  e(u32 i, u32 j, u32  k){R *(T*) Aelm(mtr, i,j,k); }
T&  e(u32 i, u32 j, u32  k, u32  v){R *(T*) Aelm(mtr, i,j,k, v); }

i32  ov(u32  a){R 0;}  // mai overflow
i32  ov(i32  a){R 0;}
i32  ov(d64  a){R a==NAN||a==+INF||a==-INF ? 1: 0; } // overflow con NAN 
i32  ov(ou32 a){R a.v==-1? 1: 0; } // overflow con -1 range: 0..0FFFFFFFE
i32  ov(oi32 a){R a.v==0x80000000? 1: 0; } // overflow con 0x80000000

Matrix<T>& operator=(Matrix<T>& a)
{u32  i;
 if(a.mtr==  0)
     {
l0:   *Led(mtr)=1; G(1,z); /* errore trovato e riportato */
     }
 G(*Led(a.mtr), l0);
 if(a.mtr!=mtr)
   {// deve essere delle stesse dimensioni e indici massimi
    G(a.mtr[0]!=mtr[0], l0);
    F(i=1;i<=mtr[0];++i)
         G(a.mtr[i]!=mtr[i], l0);
    G(*Led(a.mtr)!=0, l0); G(*SZe(mtr)!=*SZe(a.mtr), l0);
    memcpy(mtr, a.mtr, *SZ(a.mtr)+(2*a.mtr[0]+4)*sizeof(u32));
   }
z:
 R  *this;
}

Matrix<T>& operator+=(Matrix<T>& a)
{u32        i;
 T    *p1,*p2;
 u8     *pmax;

 if(a.mtr==  0)
     {
l0:   *Led(mtr)=1; G(1,z); /* errore trovato e riportato */
     }
 G(*Led(mtr), z); G(*Led(a.mtr), l0);
     // deve essere delle stesse dimensioni e indici massimi
 G(a.mtr[0]!=mtr[0], l0);
 F(i=1;i<=mtr[0];++i)
         G(a.mtr[i]!=mtr[i], l0);
 G(*Led(a.mtr)!=0, l0); G(*SZe(mtr)!=*SZe(a.mtr), l0);
 p1=(T*) Data(mtr); p2=(T*) Data(a.mtr);
 i=*SZ(mtr); pmax=(u8*) p1; pmax+=i;
 F(; p1<pmax ;++p1,++p2)
        {*p1=*p1+*p2; G(ov(*p1),l0);}
z:
 R  *this;
}

Matrix<T>& operator-=(Matrix<T>& a)
{u32        i;
 T    *p1,*p2;
 u8     *pmax;

 if(a.mtr==  0)
     {
l0:   *Led(mtr)=1; G(1,z); /* errore trovato e riportato */
     }
 G(*Led(mtr), z); G(*Led(a.mtr), l0);
 // deve essere delle stesse dimensioni e indici massimi
 G(a.mtr[0]!=mtr[0], l0);
 F(i=1;i<=mtr[0];++i)
         G(a.mtr[i]!=mtr[i], l0);
 G(*Led(a.mtr)!=0, l0); G(*SZe(mtr)!=*SZe(a.mtr), l0);
 p1=(T*) Data(mtr); p2=(T*) Data(a.mtr);
 i=*SZ(mtr); pmax=(u8*) p1; pmax+=i;
 F(; p1<pmax ;++p1,++p2)
     {*p1=*p1-*p2; G(ov(*p1),l0);}
z:
 R  *this;
}

Matrix<T>& operator*=(T& a)
{u32        i;
 T        *p1;
 u8     *pmax;

 G(*Led(mtr), z);
 p1=(T*) Data(mtr);
 i=*SZ(mtr); pmax=(u8*) p1; pmax+=i;
 F(; p1<pmax ;++p1)   
     {*p1 = *p1 * a;
      if(ov(*p1)) {*Led(mtr)=1; G(1,z);}
     }     
z:
 R  *this;
}

Matrix<T>& operator*=(T  a)
{u32        i;
 T        *p1;
 u8     *pmax;

 G(*Led(mtr), z);
 p1=(T*) Data(mtr);
 i=*SZ(mtr); pmax=(u8*) p1; pmax+=i;
 F(; p1<pmax ;++p1)   
       {*p1 = *p1 * a;
        if(ov(*p1)) {*Led(mtr)=1; G(1,z);}        
       }
z:
 R  *this;
}

// *this=a*b
// ove *this e' come a mxn e b e' nxj
i32  mul(Matrix<T>&  a, Matrix<T>&  b)
{u32  i, j, z, i0, i1;
 T    a1, a2,  s, t1, t2;

 G( *Led(mtr) , l1);
 G( *Led(a.mtr)||*Led(b.mtr), l0);
//     la 1xn si puo' moltiplicare per nx1
// ovvero jxn si puo' moltiplicare per nxm
 if(a.mtr[0]!=2||b.mtr[0]!=2||(*this).mtr[0]!=2)
     { l0: *Led(mtr)=1; l1: R  -1; }
 if( *SZe(mtr) != *SZe(a.mtr) )
                           G(1, l0);
 if(a[1]!=b[0]||(*this)[0]!=a[0]||(*this)[1]!=a[1])
                           G(1, l0);
 i0=(*this)[0]; i1=(*this)[1];
 F(i=0; i<i0; ++i)
   {F(j=0; j<i1; ++j)
      {F(z=0, s=0u; z<i1; ++z)
           {a1=a.e(i,z); a2=b.e(z,j); s=s+(a1*a2);
            G(ov(s),l0);
           }
       (*this).e(i,j)=s;
      }
   }
 R  0;
}

i32 sub(Matrix<T>&  a, Matrix<T>&  b)
{u32            i;
 T    *p1,*p2, p3;
 u8         *pmax;

 if(a.mtr==0||b.mtr==0)
     {
l0:   *Led(mtr)=1; R  -1;
     }
 G( *Led(a.mtr)||*Led(b.mtr), l0);  
 // deve essere delle stesse dimensioni e indici massimi
 G(a.mtr[0]!=mtr[0], l0);
 G(b.mtr[0]!=mtr[0], l0);
 F(i=1;i<=mtr[0];++i)
    G(a.mtr[i]!=mtr[i]||b.mtr[i]!=mtr[i], l0);
 G(*Led(a.mtr)!=0, l0);
 G(*Led(b.mtr)!=0, l0);
 G(*SZe(mtr)!=*SZe(a.mtr), l0);
 G(*SZe(mtr)!=*SZe(b.mtr), l0);
 p1=(T*)Data(mtr); p2=(T*)Data(a.mtr); p3=(T*) Data(b.mtr);
 i=*SZ(mtr); pmax=(u8*) p1; pmax+=i;
 F(; p1<pmax; ++p1, ++p2) 
        {*p1=*p2-*p3; G(ov(*p1),l0);}
 R  0;
}

i32 add(Matrix<T>&  a, Matrix<T>&  b)
{u32            i;
 T    *p1,*p2, p3;
 u8         *pmax;

 if(a.mtr==0||b.mtr==0)
     {
l0:   *Led(mtr)=1; R  -1;
     }
 // deve essere delle stesse dimensioni e indici massimi
 G( *Led(a.mtr)||*Led(b.mtr), l0);
 G(a.mtr[0]!=mtr[0], l0);
 G(b.mtr[0]!=mtr[0], l0);
 F(i=1;i<=mtr[0];++i)
    G(a.mtr[i]!=mtr[i]||b.mtr[i]!=mtr[i], l0);
 G(*Led(a.mtr)!=0, l0);
 G(*Led(b.mtr)!=0, l0);
 G(*SZe(mtr)!=*SZe(a.mtr), l0);
 G(*SZe(mtr)!=*SZe(b.mtr), l0);
 p1=(T*)Data(mtr); p2=(T*)Data(a.mtr); p3=(T*) Data(b.mtr);
 i=*SZ(mtr); pmax=(u8*) p1; pmax+=i;
 F(; p1<pmax ;++p1,++p2) 
     { *p1=*p2+*p3; G(ov(*p1),l0); }
 R  0;
}

i32  transpose(Matrix<T>&  a)
{u32   t;
 if(a.mtr==0)
     {
l0:   *Led(mtr)=1; l1: R  -1;
     }
 G(*Led(  mtr), l1); 
 G(*Led(a.mtr), l0);
 G(*SZe(mtr)!=*SZe(a.mtr), l0);
 G(a.mtr[0]!=2||mtr[0]!=2, l0);
 G(mtr[1]*mtr[2]!=a.mtr[1]*a.mtr[2], l0);
 mtr[1]=a.mtr[2]; mtr[2]=a.mtr[1];
 mtr[3]=a.mtr[4]; mtr[4]=a.mtr[3];
 R  0;
}

i32  transpose(void)
{u32  t;
 if(mtr[0]!=2)
     {
l0:   *Led(mtr)=1; l1: R  -1;
     }
 G( *Led(mtr), l1 ); 
 t=mtr[1]; mtr[1]=mtr[2]; mtr[2]=t;
 t=mtr[3]; mtr[3]=mtr[4]; mtr[4]=t;
 R 0;
}

T  det(void)
{u32                  t, i1, k;
 permutazioni<u32>  pe(mtr[1]);
 T                     x, p, s;
 s=-1;
 if(mtr[0]!=2u)
     {
l0:   *Led(mtr)=1;
l1:    R  s;
     }
 G(     *Led(mtr), l1);
 G(mtr[1]!=mtr[2], l0);
 i1=mtr[1]; s=0;
la:    F(k=0, p=1; k<i1; ++k)
           p=p*(*this).e(k, pe[k]);
       G(ov(p),l0);
       if(pe.cnt%2!=0) {s=s-p; // cout<<"-"<<(i32)p;
                       }
       else            {s=s+p; // cout<<"+"<<(i32)p;
                       }
       G(ov(s),l0);
       G( (t=pe.nextP(1))==-1, l0);
       G( t==1, la);
 R  s;
}

i32  e(void){R  MtrBadAlloc;}

i32  ini(T*  a)
{ T  *p1, *p2;
  u8    *pmax;
  u32       i;

  if(a==0)  R  -1;
  p1=(T*) Data(mtr); p2=a;
  i=*SZ(mtr); pmax=(u8*) p1; pmax+=i;
  F(; p1<pmax ;++p1, ++p2)
        *p1=*p2;
}

/* ritorna il sup dell'indice consentito */
/* indici = 0, 1, 2 ,3 ,4 */
i32  operator[](i32  i)
{if(mtr==0||mtr[0]<=i)  R  0;
 R  mtr[i+1];
}
int  print(void){PrntMtrx(mtr);}

u32    *mtr;
};


u32  f(void)
{Matrix<u32>  m1(2,2),m2(2,3),m6(2,2),m7(2,2),m8(1,2),m9(1,2);
 Matrix<d64>  m3(2,2);
 Matrix<ou32> m10(2,2);
 Matrix<u8>   m4(1,7), m5(7,1);
 Matrix<oi32> m12(4,4), m13(3,3);
 u32  d, i, j;
 i32  vm[] ={ 2,9,9,4,  2,-3,12,8,  4,8,3,-5,  1,2,6,4 }, r;
 i32  vm1[]={ 1,2,3,  4,5,6,  7,8,9};

 /* if some Matrix<T> object is not allocated return 0 */
 if(m1.e()) R  0;
 F(i=0; i<m2[0]; ++i)
   F(j=0; j<m2[1]; ++j)
        m2.e(i,j)=10*i+j;

 F(i=0; i<m4[0]; ++i)
   F(j=0; j<m4[1]; ++j)
        m4.e(i,j)=10*i+j;

 F(i=0; i<m5[0]; ++i)
   F(j=0; j<m5[1]; ++j)
         m5.e(i,j)=10*i+j;

 m1.e(0,0)=1; m1.e(0,1)=2;
 m1.e(1,0)=3; m1.e(1,1)=4;

 d=m1.det();

 m12.ini( (oi32*) vm); /* can be wrong etc */
 m13.ini( (oi32*) vm1);

 m6.e(0,0)=5; m6.e(0,1)=6;
 m6.e(1,0)=7; m6.e(1,1)=8;

 m7.mul(m1, m6);

 F(i=0; i<m3[0]; ++i)
    F(j=0; j<m3[1]; ++j)
        m3.e(i,j)=10.23*i+j;

 m8.e(0,0)=1; m8.e(0,1)=2;
 m9.mul(m8, m1);

 P("M1:\n"); m1.print(); P("\n\n");
 P("m1.det()=%d\n", d);

 P("M2:\n"); m2.print(); P("\n\n");
 P("M3:\n"); m3.print(); P("\n\n");
 P("M4:\n"); m4.print(); P("\n\n");
 P("M5:\n"); m5.print(); P("\n\n");
 P("M6:\n"); m6.print(); P("\n\n");
 P("M7=m1*m6:\n"); m7.print(); P("\n\n");
 P("M8\n"); m8.print(); P("\n\n");
 P("M9=m8*m1\n"); m9.print(); P("\n\n");
 P("M12\n");m12.print(); P("\n\n");
 r=m12.det();
 P("DET(M12)=%d\n", r);
 m7=m1;
 P("M7: m7=m1\n"); m7.print(); P("\n\n");
 m1*=10;
 P("M1=M1*10:\n"); m1.print(); P("\n\n");
 m1+=m1;
 P("M1+=M1:\n"); m1.print(); P("\n\n");
 P("M13:\n"); m13.print(); P("\n\n");
 r=m13.det();
 P("DET(M13)=%d\n", r);
 R  0;
}

i32  g(void)
{u32             i, r, j;
 permutazioni<u32>  m(3);
 permutazioni<u8>   v(4);
 char      *ptr="mare";

 if(m.e())  R  0;
 F(i=0; i<m.n; ++i)
          m[i]=i;
 F(;;){m.print();
       G( (r=m.nextP(1))==-1 || r==0,  m1);
      }
 m1:
 F(i=0; i<v.n; ++i)
         v[i]=ptr[i];

 F(;;){v.print();
       G( (r=v.nextP(1))==-1 || r==0,  la);
      }
la:
 P("\n");
 R 0;
}

int main(void)
{if( sizeof(u32)!=4 || sizeof(u16)!=2 )
       {P("Basic type error\n"); R  0;}
 f();
 R 0;
}

------------
M1:
         1          2
         3          4


m1.det()=-2
M2:
         0          1          2
        10         11         12


M3:
      0.00      1.00
     10.23     11.23


M4:
  0   1   2   3   4   5   6


M5:
  0
 10
 20
 30
 40
 50
 60


M6:
         5          6
         7          8


M7=m1*m6:
        19         22
        43         50


M8
         1          2


M9=m8*m1
         7         10


M12
         2          9          9          4
         2 4294967293         12          8
         4          8          3 4294967291
         1          2          6          4


DET(M12)=147
M7: m7=m1
         1          2
         3          4


M1=M1*10:
        10         20
        30         40


M1+=M1:
        20         40
        60         80


M13:
         1          2          3
         4          5          6
         7          8          9


DET(M13)=0

[toc] | [prev] | [next] | [standalone]


#62110

Fromasetofsymbols@gmail.com
Date2015-05-16 03:46 -0700
Message-ID<c6b652e0-0221-41ec-8685-6e8b7fd8d5fa@googlegroups.com>
In reply to#62094
I wrote:"
T  det(void) 
{u32                  t, i1, k; 
 permutazioni<u32>  pe(mtr[1]); 
 T                     x, p, s; 
 s=-1; 
 if(mtr[0]!=2u) 
     { 
l0:   *Led(mtr)=1; 
l1:    R  s; 
     } 
 G(     *Led(mtr), l1); 
 G(mtr[1]!=mtr[2], l0); 
 i1=mtr[1]; s=0; 
la:    F(k=0, p=1; k<i1; ++k) 
           p=p*(*this).e(k, pe[k]); 
       G(ov(p),l0); 
       if(pe.cnt%2!=0) {s=s-p; // cout<<"-"<<(i32)p; 
                       } 
       else            {s=s+p; // cout<<"+"<<(i32)p; 
                       } 
       G(ov(s),l0); 
       G( (t=pe.nextP(1))==-1, l0); 
       G( t==1, la); 
 R  s; 
}"

This above it is bugged
Perhaps this is better:
T  det(void) 
{u32                  t, i1, k; 
 permutazioni<u32>  pe(mtr[1]); 
 T                    rtn, p, s; 
 rtn=returnOverflowTelement(rtn);
 if(mtr[0]!=2u) 
     { 
l0:      R  rtn; 
     } 
 G(     *Led(mtr), l0); 
 G(mtr[1]!=mtr[2], l0); 
 i1=mtr[1]; s=0; 
la:    F(k=0, p=1; k<i1; ++k) 
           p=p*(*this).e(k, pe[k]); 
       G(ov(p),l0); 
       if(pe.cnt%2!=0) {s=s-p; // cout<<"-"<<(i32)p; 
                       } 
       else            {s=s+p; // cout<<"+"<<(i32)p; 
                       } 
       G(ov(s),l0); 
       G( (t=pe.nextP(1))==-1, l0); 
       G( t==1, la); 
 R  s; 
}

[toc] | [prev] | [next] | [standalone]


#62481

Fromluser droog <luser.droog@gmail.com>
Date2015-05-23 00:17 -0700
Message-ID<af24a291-b775-4a78-b444-471ac97997cb@googlegroups.com>
In reply to#61593
On Wednesday, May 6, 2015 at 1:18:25 AM UTC-5, luser droog wrote:
> I've been thinking further about the arbitrary-dimensional array
> structure (and am still stunned by the lackluster response so far),
> and I think a suitable coup-de-grace for demonstrating the value 
> of indirection is to tease-out yet another data member for this
> structure. This ties everything back to the 1962 APL book, the
> chapter on Representation, which has kept me up so many, many nights.
> 
> A dynamic array object is defined by its rank, list of dimension sizes,
> a pointer to the start of the data, AND a weighting vector which for
> simple uses merely caches the multiplications from the array-access
> formula but it affords us much greater flexibility as (I hope) we
> shall see...
> 

Posted a new write-up on SO
http://stackoverflow.com/questions/30409991/use-a-dope-vector-to-access-arbitrary-slices-of-a-multidimensional-array

-- 
y'all know how to upvote, right?

[toc] | [prev] | [next] | [standalone]


#62494

Fromasetofsymbols@gmail.com
Date2015-05-23 07:59 -0700
Message-ID<f5a37fd7-3a55-4ec0-a387-a1043e11f640@googlegroups.com>
In reply to#62481
And how do you print the values
(the elements )
of one 3 index Matrix?
or 4 index matrix? 

I had seen such matrices only in 
tensors mathematics 
That made sense after some index is lost
thru multiplication with a 
vector or some other matrix 

[toc] | [prev] | [next] | [standalone]


#62512

Fromasetofsymbols@gmail.com
Date2015-05-23 21:25 -0700
Message-ID<530bcd6f-6f4a-4f39-a048-1e288c1ffec2@googlegroups.com>
In reply to#62494
So where are useful these >2 dimensions matrices?

[toc] | [prev] | [next] | [standalone]


#62574

Fromluser droog <luser.droog@gmail.com>
Date2015-05-24 18:29 -0700
Message-ID<1b179fdb-941e-4543-9111-be9ae6cec426@googlegroups.com>
In reply to#62512
On Saturday, May 23, 2015 at 11:26:07 PM UTC-5, asetof...@gmail.com wrote:
> So where are useful these >2 dimensions matrices?

I find this to be a very difficult question to answer without some
handwaving. Frankly, wherever you have >1 of these 2d objects that
must be treated as a unit. 

But more peosaically, 

int seraphim=3,
    cherubim=3,
    thrones=3,
    dominions=3,
    powers=3,
    authorities=3,
    principalities=3,
    archangels=3,
    angels=3;

arr aleph = array(seraphim,
                  cherubim,
                  thrones,
                  dominions,
                  powers,
                  authorities,
                  principalities,
                  archangels,
                  angels);

and eternitie in an houre.

[toc] | [prev] | [next] | [standalone]


#62570

Fromluser droog <luser.droog@gmail.com>
Date2015-05-24 18:08 -0700
Message-ID<c5c0aeec-b3d6-4a0b-96cb-7ad13e31cf38@googlegroups.com>
In reply to#62494
On Saturday, May 23, 2015 at 9:59:39 AM UTC-5, asetof...@gmail.com wrote:
> And how do you print the values
> (the elements )
> of one 3 index Matrix?
> or 4 index matrix? 
> 
> I had seen such matrices only in 
> tensors mathematics 
> That made sense after some index is lost
> thru multiplication with a 
> vector or some other matrix

Alright, I came up with some code to print them.
Until now, I've just been printing them "manually"
while testing. And the printing functions in all 
the versions of inca are pretty terrible and limited.

I tried to write this recursively/inductively, so it
*ought* to be correct, without extensive testing,
because it *looks* correct. I think. If I did it right.

And you're right about the relation to tensor
mathematics. Iverson borrowed lots of ideas there.

other functions at 
https://github.com/luser-dr00g/inca/blob/master/arrind.c

int iscontiguous(arr a){ 
    int i,x;
    for (i=a->rank-1,x=1; i>=0; i--){
        if (a->weight[i] != x)
            return 0;
        x *= a->dims[i];
    }   
    return 1;
}

void print(arr a, int width){
    int i;
    int maxwidth;
    int freecopy = 0;

    if (width){
        maxwidth=width;
    } else {
        int datasz = productdims(a->rank,a->dims);

        if (!iscontiguous(a)) {
            a = copy(a);
            freecopy = 1;
        }

        maxwidth=0;
        for (i=0; i<datasz; i++){
            int size = snprintf(NULL,0,"%d",a->data[i]);
            if (size > maxwidth)
                maxwidth = size;
        }
    }   

    switch(a->rank){
    case 0: printf("%*d\n", maxwidth, *a->data);
            break;
    case 1: for (i=0; i<a->dims[0]; i++)
                printf("%*d ", maxwidth, *elem(a,i));
            break;
    default:
            for (i=0; i<a->dims[0]; i++){
                arr as = slice(a,i);
                print(as,maxwidth);
                printf("\n");
                free(as);
            }
            break;
    }

    if (freecopy)
        free(a);
}


int main() {

#ifdef TEST_PRINT
    {
        arr a = iota(27);
        arr b = casta(a->data, 3, (int[]){3,3,3});
        print(b,0);

        free(b);
        free(a);

        a = iota(64);
        b = casta(a->data, 4, (int[]){2,2,2,2});
        print(b,0);

        free(b);
        free(a);
    }
#endif

    return 0;
}


produces:

 0  1  2 
 3  4  5 
 6  7  8 

 9 10 11 
12 13 14 
15 16 17 

18 19 20 
21 22 23 
24 25 26 

 0  1 
 2  3 

 4  5 
 6  7 


 8  9 
10 11 

12 13 
14 15 


[toc] | [prev] | [next] | [standalone]


#62582

Fromasetofsymbols@gmail.com
Date2015-05-25 00:13 -0700
Message-ID<e39ad19c-1288-4b8d-9c09-c7dc8811ca99@googlegroups.com>
In reply to#62570
luser droog wrote:...

Thank you

[toc] | [prev] | [standalone]


Page 3 of 3 — ← Prev page 1 2 [3]

Back to top | Article view | comp.lang.c


csiph-web