Vector class using template expressions

xiaoxiao2021-03-06  88

// numeric.h

#ifndef _numeric_h_

#define _numeric_h_

#include // memcpy

#include // std ::

Using namespace std;

Namespace numeric {

#define max (A, B) (((a)> (b))? (a): (b))

#define min (a, b) (((a)> (b))? (b): (a))

Template

Class expr_add

{

PUBLIC:

Static Inline T Apply (Const T & A, Const T & B)

{

RETURN A B;

}

}

Template

Class expr_sub

{

PUBLIC:

Static Inline T Apply (Const T & A, Const T & B)

{

Return A - B;

}

}

Template

Class expr_mul

{

PUBLIC:

Static Inline T Apply (Const T & A, Const T & B)

{

Return a * b;

}

}

Template

Class expr_div

{

PUBLIC:

Static Inline T Apply (Const T & A, Const T & B)

{

Return A / B;

}

}

Template

CLASS TPL_EXPR

{

Const A & A_;

CONST B & B_;

PUBLIC:

TPL_EXPR (Const A & A, Const B): A_ (a), b_ (b) {}

Inline Double Operator [] (SIZE_T I) Const

{

Return Op :: Apply (A_ [i], b_ [i]);

}

}

Template

Class TPL_EXPR

{

Const Double & A_;

CONST B & B_;

PUBLIC:

TPL_EXPR (Const Double & A, Const B): A_ (a), b_ (b) {}

Inline Double Operator [] (SIZE_T I) Const

{

Return op :: Apply (A_, B_ [i]);

}

}

Template

Class TPL_EXPR

{

Const Int & A_;

CONST B & B_;

PUBLIC:

TPL_EXPR (Const Int & A, Const B & B): A_ (a), b_ (b) {}

Inline Double Operator [] (SIZE_T I) Const

{

Return op :: Apply (A_, B_ [i]);

}

}

Template

Class TPL_EXPR

{

Const A & A_;

Const Double & B_;

PUBLIC:

TPL_EXPR (Const A & A, Const Double & B): A_ (a), b_ (b) {}

Inline Double Operator [] (SIZE_T I) Const

{

Return op :: Apply (A_ [i], b_);

}

}

Template

Class TPL_EXPR

{

Const A & A_;

Const Int & B_;

PUBLIC:

TPL_EXPR (Const A & A, Const Int & b): A_ (a), b_ (b) {}

Inline Double Operator [] (SIZE_T I) Const

{

Return op :: Apply (A_ [i], b_);

}

}

Template

TPL_EXPR > Operator (Const A & A, Const B & B)

{

Return TPL_EXPR > (A, B);

}

Template

TPL_EXPR > Operator- (Const A & A, Const B & B)

{

Return TPL_EXPR > (A, B);

}

Template

TPL_EXPR > Operator * (Const A & A, Const B & B)

{

RETURN TPL_EXPR > (A, B);

}

Template

TPL_EXPR > Operator / (Const A & A, Const B & B)

{

RETURN TPL_EXPR >> (A, B);

}

/

/

Class vector

{

protected:

SIZE_T DIM;

Double * ETS;

PUBLIC:

// constructed by the number of known numerals and vector elements to construct a vector

VECTOR (size_t n, const double * const pd)

: DIM (n)

{

ETS = New Double [DIM];

Memcpy (ETS, PD, DIM * SIZEOF (DOUBLE));

}

Vector (size_t n = 0, double d = 0.0)

: DIM (n)

{

ETS = New Double [DIM];

For (SIZE_T I = 0; i

ETS [I] = D;

}

}

VECTOR (Const Vector & V)

{

DIM = v.dim;

ETS = New Double [DIM];

Memcpy (ETS, V. ETS, SIZEOF (DOUBLE) * DIM);

}

Vector & Operator = (Const Vector & V)

{

IF (this! = & v) {// prevent yourself from copying yourself (DIM! = v.dim) {

Exit (1);

}

Memcpy (ETS, V. ETS, SIZEOF (DOUBLE) * DIM);

}

RETURN * THIS;

}

~ vector ()

{

DELETE [] ETS;

}

PUBLIC:

Inline Double Operator [] (SIZE_T I) Const

{

Return ETS [I];

}

Inline Double & Operator [] (SIZE_T i)

{

Return ETS [I];

}

PUBLIC:

SIZE_T GET_DIM () Const

{

Return Dim;

}

Double * get_ptr () const

{

Return ETS;

}

Double Sum_Element () Const

{

Double TMP = 0;

For (SIZE_T I = 0; i

TMP = ETS [i];

}

Return TMP;

}

Double min_eray () const

{

Double TMP = ETS [0];

For (SIZE_T I = 0; i

IF (TMP> ETS [I]) {

TMP = ETS [I];

}

}

Return TMP;

}

Double Max_Element () Const

{

Double TMP = ETS [0];

For (SIZE_T I = 0; i

IF (TMP

TMP = ETS [I];

}

}

Return TMP;

}

PUBLIC:

Vector & Operator ()

{

RETURN * THIS;

}

Vector & Operator- ()

{

For (SIZE_T I = 0; i

ETS [i] = -ets [i];

}

RETURN * THIS;

}

PUBLIC:

Vector & Operator = (Const Vector & V)

{

For (SIZE_T I = 0; i

ETS [i] = v [i];

}

RETURN * THIS;

}

Vector & Operator - = (Const Vector & V)

{

For (SIZE_T I = 0; i

ETS [i] - = v [i];

}

RETURN * THIS;

}

Vector & Operator * = (Const Vector & V)

{

For (SIZE_T I = 0; i

ETS [i] * = v [i];

}

RETURN * THIS;

}

Vector & Operator / = (Const Vector & V)

{

For (SIZE_T I = 0; i

ETS [i] / = v [i];

}

RETURN * THIS;

}

Vector & Operator = (Const Double & D)

{

For (SIZE_T I = 0; i

ETS [i] = d;}

RETURN * THIS;

}

Vector & Operator - = (Const Double & D)

{

For (SIZE_T I = 0; i

ETS [I] - = D;

}

RETURN * THIS;

}

Vector & Operator * = (Const Double & D)

{

For (SIZE_T I = 0; i

ETS [i] * = D;

}

RETURN * THIS;

}

Vector & Operator / = (Const Double & D)

{

Double TMP = 1 / D;

For (SIZE_T I = 0; i

ETS [I] * = TMP;

}

RETURN * THIS;

}

PUBLIC:

Template

Vector & Operator = (const TPL_EXPR & Expr)

{

For (SIZE_T I = 0; i

ETS [i] = expr [i];

}

RETURN * THIS;

}

Template

Vector & Operator = (Const TPL_EXPR & Expr)

{

For (SIZE_T I = 0; i

ETS [i] = expr [i];

}

RETURN * THIS;

}

Template

Vector & Operator - = (Const TPL_EXPR & Expr)

{

For (SIZE_T I = 0; i

ETS [i] - = expr [i];

}

RETURN * THIS;

}

Template

Vector & Operator * = (const TPL_EXPR & Expr)

{

For (SIZE_T I = 0; i

ETS [i] * = expr [i];

}

RETURN * THIS;

}

Template

Vector & operator / = (Const TPL_EXPR & Expr)

{

For (SIZE_T I = 0; i

ETS [i] / = expr [i];

}

RETURN * THIS;

}

}

}

#ENDIF / / _NUMERIC_H_

// Test.cpp

#include

#include

#include

Using namespace std;

#include "numeric.h"

Using Namespace Numeric;

Void main () {

INT m = 10000, n = 10000;

// Vector x (m, 1), y (m, 2), z (m, 3);

Valarray x (1, m), y (2, m), z (3, m);

Clock_t time;

TIME = clock ();

For (size_t i = 0; i

Z = 0.5- (x - y) * 1.0;

}

Time = clock () - TIME;

COUT << z [0] << endl;

COUT << Time / 1000.0 << endl;

}

转载请注明原文地址:https://www.9cbs.com/read-106862.html

New Post(0)