forcal 发表于 2011-10-20 09:57

Lu中的运算符重载

    在Lu中可以很方便地对运算符进行重载。例如:thetype(x,y,num,op)=which
{
  op<0  : return,
  op==0 : x-y,    //重载运算符+
  op==1 : x+y,    //重载运算符-
  op==2 : x/y,    //重载运算符*
  nil             //该数据类型不支持该运算符的重载,返回nil
};
test(:type,a,b)=
  type=thetype(0,0,0,-1),  //获取新数据类型
  a=cast, b=cast,  //强制转换为新数据类型
  o[" a=",3," b=",5],  //输出a和b
  o[" a+b=",a+b],      //计算并输出a+b,变成了a-b
  o[" a-b=",a-b],      //计算并输出a-b,变成了a+b
  o[" a$b=",a$b];      //没有重载运算符$,故输出nil结果: a=3 b=5 a+b=-2 a-b=8 a$b=nil======

  Lu核心库中没有提供矩阵运算,但在脚本中可以通过重载运算符来实现:outm(x:i,j,m,n)= //输出一个矩阵
{
        len,
        i=0, while{i<m,
                o["\r\n"], j=0, while{j<n, o, j++},
                i++
        },
        o["\r\n"], x
};
mymatrix(x,y,num,op:c,i,j,k,m,n,u)=which //定义矩阵运算
{
        op<0  : return,
        op==0 :        //重载运算符+
        {
                len, c=new.global(),
                i=0, while{i<m,
                        j=0, while{j<n,
                                c=x+y,
                                j++
                        },
                        i++
                },
                c
        },
        op==1 :        //重载运算符-
        {
                len, c=new.global(),
                i=0, while{i<m,
                        j=0, while{j<n,
                                c=x-y,
                                j++
                        },
                        i++
                },
                c
        },
        op==2 :        //重载运算符*
        {
                len, len, c=new.global(),
                i=0, while{i<m,
                        j=0, while{j<k,
                                c=0.0,
                                u=0, while{u<n,
                                        c=c+x*y, u++
                                },
                                j++
                        },
                        i++
                },
                c
        },
        op==25 ://重载运算符.*
        {
                len, c=new.global(),
                i=0, while{i<m,
                        j=0, while{j<n,
                                c=x*y,
                                j++
                        },
                        i++
                },
                c
        },
        op==26 ://重载运算符./
        {
                len, c=new.global(),
                i=0, while{i<m,
                        j=0, while{j<n,
                                c=x/y,
                                j++
                        },
                        i++
                },
                c
        },
        nil        //该数据类型不支持该运算符的重载,返回nil
};
test(:type,a,b,c)=
        type=mymatrix(0,0,0,-1),  //获取新数据类型
        a=new,  //生成矩阵a
        b=new,  //生成矩阵b
        c=new,  //生成矩阵c
        o["a="], outm(a), o["b="], outm(b), o["c="], outm(c),  //输出a、b和c
        a=cast, b=cast,  //强制转换为新数据类型
        o["a+b="], outm,     //计算并输出a+b
        o["a-b="], outm,     //计算并输出a-b
        o["a*c="], outm,     //计算并输出a*c
        o["a.*b="],outm,    //计算并输出a.*b
        o["a./b="],outm;    //计算并输出a./b结果:a=
0.  1.  2.  
3.  4.  5.  
b=
1.  2.  3.  
4.  5.  6.  
c=
6.  7.  
8.  9.  
0.  1.  
a+b=
1.  3.  5.  
7.  9.  11.  
a-b=
-1.  -1.  -1.  
-1.  -1.  -1.  
a*c=
8.  11.  
50.  62.  
a.*b=
0.  2.  6.  
12.  20.  30.  
a./b=
0.  0.5  0.66666666666666663  
0.75  0.80000000000000004  0.83333333333333337

forcal 发表于 2011-10-22 07:43

上面关于矩阵运算的运算符重载例子中,函数o和new也是可以重载的:mymatrix(x,y,z,para,num,op:c,i,j,k,m,n,u,static,me)=which //定义矩阵运算
{
        op<0  : return,
        op==0 : //重载运算符+
        {
                len, c=new.global(),
                i=0, while{i<m,
                        j=0, while{j<n,
                                c=x+y,
                                j++
                        },
                        i++
                },
                cast  //强制转换为新类型me(矩阵),下同
        },
        op==1 : //重载运算符-
        {
                len, c=new.global(),
                i=0, while{i<m,
                        j=0, while{j<n,
                                c=x-y,
                                j++
                        },
                        i++
                },
                cast
        },
        op==2 : //重载运算符*
        {
                len, len, c=new.global(),
                i=0, while{i<m,
                        j=0, while{j<k,
                                c=0.0,
                                u=0, while{u<n,
                                        c=c+x*y, u++
                                },
                                j++
                        },
                        i++
                },
                cast
        },
        op==25 ://重载运算符.*
        {
                len, c=new.global(),
                i=0, while{i<m,
                        j=0, while{j<n,
                                c=x*y,
                                j++
                        },
                        i++
                },
                cast
        },
        op==26 : //重载运算符./
        {
                len, c=new.global(),
                i=0, while{i<m,
                        j=0, while{j<n,
                                c=x/y,
                                j++
                        },
                        i++
                },
                cast
        },
        op==46 : //重载函数new
        {
                c=new.global(), m=len, k=0,
                i=0, while{i<y,
                        j=0, while{j<z, if{k>=m, return}, c(i,j)=para, j++},
                        i++
                },
                cast
        },
        op==49 : //重载函数o
        {
                len, k=0,
                i=0, while{i<m,
                        o["\r\n"], k=k+2, j=0, while{j<n, k=k+o, j++},
                        i++
                },
                o["\r\n"], k+2
        },
        nil     //该数据类型不支持该运算符的重载,返回nil
};
test(:type,a,b,c)=
        type=mymatrix(0,0,0,0,0,-1),  //获取新数据类型
        a=new],  //生成矩阵a
        b=new],  //生成矩阵b
        c=new],  //生成矩阵c
        o["a=",a, "b=", b, "c=", c],  //输出a、b和c
        o["a+b=", a+b],     //计算并输出a+b
        o["a-b=", a-b],     //计算并输出a-b
        o["a*c=", a*c],     //计算并输出a*c
        o["a.*b=",a.*b],    //计算并输出a.*b
        o["a./b=",a./b];    //计算并输出a./b结果:a=
0.  1.  2.  
3.  4.  5.  
b=
1.  2.  3.  
4.  5.  6.  
c=
6.  7.  
8.  9.  
0.  1.  
a+b=
1.  3.  5.  
7.  9.  11.  
a-b=
-1.  -1.  -1.  
-1.  -1.  -1.  
a*c=
8.  11.  
50.  62.  
a.*b=
0.  2.  6.  
12.  20.  30.  
a./b=
0.  0.5  0.66666666666666663  
0.75  0.80000000000000004  0.83333333333333337  当然,在脚本中实现这些重载只是玩弄技巧,用C/C++实现这种重载才是王道,对此,Lu核心库提供了更好的支持。

www.tgchaye.com 发表于 2011-12-4 03:38

新的一天,心的祝福,祝福健康快乐!欢迎到我博文,喜欢的话请多多关注我吧
页: [1]
查看完整版本: Lu中的运算符重载