Jarod 的学习笔记

统计

留言簿(1)

阅读排行榜

评论排行榜

未完成东西中的完成部分

这是上次PPT所应该讲述的代码内容。现在放上来,大家就应该清楚PPT未完成的部分所想要描述的技术了。PS:代码中还是有些没用的东西。

template <typename LeftExp, typename Op, typename RightExp>
struct Expression {
    
const LeftExp & left;
    
const RightExp & right;
    
    Expression(
const LeftExp & _l, const RightExp & _r) : left(_l), right(_r){}

    
    
double operator [] (int index) const {
        
return Op::eval(left[index], right[index]);
    }

}
;

struct EPLUS {
    
static double eval(double a, double b) return a+b;}
}
;

class Matrix 
{
private:
    
int M,N;
    
double * element;
public:
    Matrix(
int m, int n): M(m),N(n) {
        element 
= new double[M*N];
    }

    
~Matrix(){
        
if (element) {
            delete []element;
            element 
= 0;
        }

    }


    
int getM() const return M;}
    
int getN() const return N;}
    
int getSize() const {return M*N;}
    
    
double & operator ()(int i, int j) const return element[i*N+j]; }
    
double operator [](int index) const {return element[index]; }
    
    template 
<typename Exp>
    Matrix 
& operator = (Exp & exp) {    
        
int size = M*N;
        
for (int i=0;i<size;i++{
            element[i] 
= exp[i];
        }

        
return * this;
    }


template 
<typename LeftExp, typename RightExp>
inline Expression
<LeftExp,EPLUS, RightExp> operator + (const LeftExp & a, const RightExp & b) 
    
return Expression<LeftExp, EPLUS, RightExp>(a, b);
}


int main() {
    
const int M=3, N=4;
    Matrix a(M,N), b(M,N), c(M,N);
    
{
        
for (int i=0;i<M;i++
            
for (int j=0;j<N;j++{
                a(i,j) 
= i+j;
                b(i,j) 
= i*j;
                c(i,j) 
= 0;
            }

    }

    c 
= a + b;
    
return 0;
}

posted on 2007-03-29 01:51 Jarod-Yin 阅读(644) 评论(2)  编辑 收藏 引用

评论

# re: 未完成东西中的完成部分 2007-03-29 12:54 chenger

看到了expression template……确实是好技术,虽然用得不多。  回复  更多评论   

# re: 未完成东西中的完成部分 2007-03-29 14:00 skipper

缓式评估的问题是提出来了,但是你没有提出lazy evaluation的算法啊?究竟应该怎么实现呢?  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理