Skip to content

Commit 90cc9de

Browse files
committed
Correct bug in formal -,./,./ of array or Matrix (missing operator)
add missing unary in formal arry operation +,- `(Thank To O. Pantz).
1 parent 21f80e1 commit 90cc9de

File tree

2 files changed

+104
-2
lines changed

2 files changed

+104
-2
lines changed

examples/tutorial/array.edp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,3 +481,27 @@ cout << vh.n <<endl;
481481

482482

483483
}
484+
{// Sep 2025 verif new formal operator
485+
func cId =
486+
[ [1.i,0.],
487+
[0.,1.i]];
488+
func Z =
489+
[ [0,0.],
490+
[0.,0]];
491+
492+
complex[int,int] I0 =-cId';
493+
complex[int,int] I1 =cId-cId';
494+
complex[int,int] I2 = cId-cId;
495+
complex[int,int] I3 = cId+cId;
496+
497+
// verif
498+
499+
cout << I0.sum << " "<< I0 << endl
500+
<< I1.sum << " "<< I1 << endl
501+
<< I2.sum << " "<<I2 << endl
502+
<< I3.sum<< " "<< I3<< endl;
503+
assert(I0.sum== 2i);
504+
assert(I1.sum==4i);
505+
assert(I2.sum==0);
506+
assert(I3.sum==4i);
507+
}

src/fflib/AFunction.cpp

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,17 @@ class opTrans : public OneOperator{
595595
E_F0 * code(const basicAC_F0 & args) const {
596596
return new TransE_Array(dynamic_cast<const E_Array*>((Expression) args[0])); }
597597
};
598+
class opUnary : public OneOperator{
599+
public:
600+
const char * op;
601+
AnyType operator()(Stack s) const {ffassert(0);return 0L;}
602+
bool MeshIndependent() const { return false;}
603+
604+
opUnary(const char * oop,aType A): OneOperator(atype<C_F0>(),A), op(oop) {}
605+
606+
E_F0 * code(const basicAC_F0 & ) const {ffassert(0);}
607+
C_F0 code2(const basicAC_F0 &args) const;
608+
};
598609

599610
class opDot : public OneOperator{
600611
public:
@@ -1422,6 +1433,11 @@ void Init_map_type()
14221433
TheOperators->Add("./",new opSum("/",atype<E_Array >(),atype<TransE_Array>() ) ); // a faire mais dur
14231434
// correct in sept. 2009
14241435
TheOperators->Add("./",new opSum("/",atype<TransE_Array >(),atype<TransE_Array>() ) ); // a faire mais dur
1436+
// Add sep 2025
1437+
TheOperators->Add("-",new opUnary("-",atype<E_Array >()) );
1438+
TheOperators->Add("-",new opUnary("-",atype<TransE_Array >()) );
1439+
TheOperators->Add("+",new opUnary("+",atype<E_Array >()) ); //
1440+
TheOperators->Add("+",new opUnary("+",atype<TransE_Array >()) );
14251441

14261442

14271443
// il faut refechir ..... FH
@@ -1926,6 +1942,7 @@ C_F0 opDot::code2(const basicAC_F0 &args) const
19261942
return C_F0();
19271943

19281944
}
1945+
19291946
C_F0 opColumn::code2(const basicAC_F0 &args) const
19301947
{
19311948
bool ta =args[0].left()==atype<TransE_Array>();
@@ -2045,11 +2062,72 @@ C_F0 opColumn::code2(const basicAC_F0 &args) const
20452062
else ffassert(0);
20462063
return C_F0();
20472064
}
2065+
C_F0 opUnary::code2(const basicAC_F0 &args) const
2066+
{
2067+
bool ta =args[0].left()==atype<TransE_Array>();
2068+
const TransE_Array * tea=0;
2069+
const E_Array * ea=0;
2070+
if( ta) tea = dynamic_cast<const TransE_Array*>((Expression) args[0]);
2071+
else ea = dynamic_cast<const E_Array*>((Expression) args[0]);
2072+
assert( ea || tea );
2073+
const E_Array & a= ta ? *tea->v : *ea;
2074+
int na=a.size();
2075+
if(na <1 && nb < 1) CompileError(" empty array - [ ...] ");
2076+
bool maa= a[0].left()==atype<E_Array>();
2077+
int ma =1;
2078+
2079+
if(maa) {
2080+
ma= a[0].LeftValue()->nbitem();
2081+
for (int i=1;i<na;i++)
2082+
if( ma != (int) a[i].LeftValue()->nbitem())
2083+
CompileError(" first matrix with variable number of columm");
2084+
2085+
}
2086+
int na1=na,ma1=ma;
2087+
if(ta) RNM::Exchange(na1,ma1);
2088+
2089+
KNM<CC_F0> A(na1,ma1) ;
2090+
if(maa)
2091+
for (int i=0;i<na;++i)
2092+
{
2093+
const E_Array * li= dynamic_cast<const E_Array *>(a[i].LeftValue());
2094+
ffassert(li);
2095+
for (int j=0; j<ma;++j)
2096+
if(!ta) A(i,j) = (*li)[j];
2097+
else A(j,i) = TryConj((*li)[j]);
2098+
}
2099+
else
2100+
for (int i=0;i<na;++i)
2101+
if(!ta) A(i,0) = a[i];
2102+
else A(0,i) = TryConj(a[i]);
20482103

2104+
2105+
AC_F0 v;
2106+
v = 0; // empty
2107+
if( ma1 == 1) // 1 vecteur
2108+
{
2109+
for (int i=0;i<na;++i)
2110+
v += C_F0(TheOperators,op,ta ? TryConj(a[i]) : a[i]) ;
2111+
return C_F0(TheOperators,"[]",v);
2112+
}
2113+
else {
2114+
// return formal matrix
2115+
AC_F0 w,v;
2116+
w=0;
2117+
for (int i=0;i<na1;++i)
2118+
{
2119+
v=0;
2120+
for (int j=0;j<ma1;++j)
2121+
v+= C_F0(TheOperators,op,A(i,j));
2122+
w+=C_F0(TheOperators,"[]",v);
2123+
}
2124+
return C_F0(TheOperators,"[]",w);
2125+
}
2126+
}
20492127

20502128
C_F0 opSum::code2(const basicAC_F0 &args) const
20512129
{
2052-
2130+
20532131
bool ta =args[0].left()==atype<TransE_Array>();
20542132
bool tb = args[1].left()==atype<TransE_Array>();
20552133
const TransE_Array * tea=0;
@@ -2137,7 +2215,7 @@ C_F0 opSum::code2(const basicAC_F0 &args) const
21372215
{
21382216
v=0;
21392217
for (int j=0;j<ma1;++j)
2140-
v+= C_F0(TheOperators,"+",A(i,j),B(i,j));
2218+
v+= C_F0(TheOperators,op,A(i,j),B(i,j));// correction 3/9/25 FH thanks to O. Pantz
21412219
w+=C_F0(TheOperators,"[]",v);
21422220
}
21432221
return C_F0(TheOperators,"[]",w);

0 commit comments

Comments
 (0)