static IA_DiscreteImage<ATYPE>
vec_vec_bo_OPNAME(const IA_VectorDI<LTYPE> *lhs,const IA_VectorDI<RTYPE> *rhs)
{
    const IA_IntPointSet domain = lhs->domain();
    const unsigned sz = domain.card();
    ATYPE *const vec = new ATYPE[sz];

    {
	ATYPE *ds = vec;
	const LTYPE *ls = lhs->vec;
	const RTYPE *rs = rhs->vec;
	for (unsigned i=0; i<sz; i++) {
	    *(ds++) = ( (*(ls++)) SUBOP (*(rs++)) );
	}
    }
    return IA_DiscreteImage<ATYPE>(domain, vec, sz, 1);
}

#ifndef vec_scalar_ATYPE_LTYPE_RTYPE_OPNAME
#define vec_scalar_ATYPE_LTYPE_RTYPE_OPNAME
static IA_DiscreteImage<ATYPE>
vec_scalar_bo_OPNAME(const IA_VectorDI<LTYPE> *lhs,RTYPE rhs)
{
    const IA_IntPointSet domain = lhs->domain();
    const unsigned sz = domain.card();
    ATYPE *const vec = new ATYPE[sz];

    {
	ATYPE *ds = vec;
	const LTYPE *ls = lhs->vec;
	for (unsigned i=0; i<sz; i++) {
	    *(ds++) = ( (*(ls++)) SUBOP (rhs) );
	}
    }
    return IA_DiscreteImage<ATYPE>(domain, vec, sz, 1);
}
#endif // vec_scalar_ATYPE_LTYPE_RTYPE_OPNAME

static IA_DiscreteImage<ATYPE>
vec_iter_bo_OPNAME(const IA_VectorDI<LTYPE> *lhs,IA_DiscreteImage<RTYPE> rhs)
{
    const IA_IntPointSet domain = lhs->domain();
    const unsigned sz = domain.card();
    ATYPE *const vec = new ATYPE[sz];

    {
	ATYPE *ds = vec;
	const LTYPE *ls = lhs->vec;
	RTYPE rtmp;
	IA_DIVIter<RTYPE>	riter(rhs);
	for (unsigned i=0; i<sz; i++) {
	    riter(rtmp);
	    *(ds++) = ( (*(ls++)) SUBOP (rtmp) );
	}
    }
    return IA_DiscreteImage<ATYPE>(domain, vec, sz, 1);
}

#ifndef scalar_vec_ATYPE_LTYPE_RTYPE_OPNAME
#define scalar_vec_ATYPE_LTYPE_RTYPE_OPNAME
static IA_DiscreteImage<ATYPE>
scalar_vec_bo_OPNAME(LTYPE lhs,const IA_VectorDI<RTYPE> *rhs)
{
    const IA_IntPointSet domain = rhs->domain();
    const unsigned sz = domain.card();
    ATYPE *const vec = new ATYPE[sz];

    {
	ATYPE *ds = vec;
	const RTYPE *rs = rhs->vec;
	for (unsigned i=0; i<sz; i++) {
	    *(ds++) = ( (lhs) SUBOP (*(rs++)) );
	}
    }
    return IA_DiscreteImage<ATYPE>(domain, vec, sz, 1);
}
#endif // scalar_vec_ATYPE_LTYPE_RTYPE_OPNAME

// scalar-scalar operation is trivial

#ifndef scalar_iter_ATYPE_LTYPE_RTYPE_OPNAME
#define scalar_iter_ATYPE_LTYPE_RTYPE_OPNAME
static IA_DiscreteImage<ATYPE>
scalar_iter_bo_OPNAME(LTYPE lhs,IA_DiscreteImage<RTYPE> rhs)
{
    const IA_IntPointSet domain = rhs.domain();
    const unsigned sz = domain.card();
    ATYPE *const vec = new ATYPE[sz];

    {
	ATYPE *ds = vec;
	RTYPE rtmp;
	IA_DIVIter<RTYPE>	riter(rhs);
	for (unsigned i=0; i<sz; i++) {
	    riter(rtmp);
	    *(ds++) = ( (lhs) SUBOP (rtmp) );
	}
    }
    return IA_DiscreteImage<ATYPE>(domain, vec, sz, 1);
}
#endif // scalar_iter_ATYPE_LTYPE_RTYPE_OPNAME

static IA_DiscreteImage<ATYPE>
iter_vec_bo_OPNAME(IA_DiscreteImage<LTYPE> lhs,const IA_VectorDI<RTYPE> *rhs)
{
    const IA_IntPointSet domain = lhs.domain();
    const unsigned sz = domain.card();
    ATYPE *const vec = new ATYPE[sz];

    {
	ATYPE *ds = vec;
	LTYPE ltmp;
	IA_DIVIter<LTYPE>	liter(lhs);
	const RTYPE *rs = rhs->vec;
	for (unsigned i=0; i<sz; i++) {
	    liter(ltmp);
	    *(ds++) = ( (ltmp) SUBOP (*(rs++)) );
	}
    }
    return IA_DiscreteImage<ATYPE>(domain, vec, sz, 1);
}

#ifndef iter_scalar_ATYPE_LTYPE_RTYPE_OPNAME
#define iter_scalar_ATYPE_LTYPE_RTYPE_OPNAME
static IA_DiscreteImage<ATYPE>
iter_scalar_bo_OPNAME(IA_DiscreteImage<LTYPE> lhs,RTYPE rhs)
{
    const IA_IntPointSet domain = lhs.domain();
    const unsigned sz = domain.card();
    ATYPE *const vec = new ATYPE[sz];

    {
	ATYPE *ds = vec;
	LTYPE ltmp;
	IA_DIVIter<LTYPE>	liter(lhs);
	for (unsigned i=0; i<sz; i++) {
	    liter(ltmp);
	    *(ds++) = ( (ltmp) SUBOP (rhs) );
	}
    }
    return IA_DiscreteImage<ATYPE>(domain, vec, sz, 1);
}
#endif // iter_scalar_ATYPE_LTYPE_RTYPE_OPNAME

static IA_DiscreteImage<ATYPE>
iter_iter_bo_OPNAME(IA_DiscreteImage<LTYPE> lhs,IA_DiscreteImage<RTYPE> rhs)
{
    const IA_IntPointSet domain = lhs.domain();
    const unsigned sz = domain.card();
    ATYPE *const vec = new ATYPE[sz];

    {
	ATYPE *ds = vec;
	LTYPE ltmp;
	IA_DIVIter<LTYPE>	liter(lhs);
	RTYPE rtmp;
	IA_DIVIter<RTYPE>	riter(rhs);
	for (unsigned i=0; i<sz; i++) {
	    liter(ltmp);
	    riter(rtmp);
	    *(ds++) = ( (ltmp) SUBOP (rtmp) );
	}
    }
    return IA_DiscreteImage<ATYPE>(domain, vec, sz, 1);
}

IA_DiscreteImage<ATYPE>
OP ( const IA_DiscreteImage<LTYPE> &lhs,
	      const IA_DiscreteImage<RTYPE> &rhs)
{
    if (lhs.domain() != rhs.domain()) {
	static IA_DiscreteImage<ATYPE>	rval;
	ia_throw( IA::PSET_MISMATCH, __FILE__,__LINE__);
	return rval;
    }

    if (lhs.type() == IA_VectorDI<LTYPE>::s_type()) {
	if (rhs.type() == IA_VectorDI<RTYPE>::s_type()) {
	    return vec_vec_bo_OPNAME((IA_VectorDI<LTYPE> *)lhs.bdip,(IA_VectorDI<RTYPE> *)rhs.bdip);
	} else if (rhs.type() == IA_ConstDI<RTYPE>::s_type()) {
	    return vec_scalar_bo_OPNAME((IA_VectorDI<LTYPE> *)lhs.bdip,((IA_ConstDI<RTYPE> *)rhs.bdip)->value);
	} else {
	    return vec_iter_bo_OPNAME((IA_VectorDI<LTYPE> *)lhs.bdip,rhs);
	}
    } else if (lhs.type() == IA_ConstDI<LTYPE>::s_type()) {
	if (rhs.type() == IA_VectorDI<RTYPE>::s_type()) {
	    return scalar_vec_bo_OPNAME(((IA_ConstDI<LTYPE> *)lhs.bdip)->value,(IA_VectorDI<RTYPE> *)rhs.bdip);
	} else if (rhs.type() == IA_ConstDI<RTYPE>::s_type()) {
	    return IA_DiscreteImage<ATYPE> (lhs.domain(), (((IA_ConstDI<LTYPE> *)lhs.bdip)->value) SUBOP (((IA_ConstDI<RTYPE> *)rhs.bdip)->value));
	} else {
	    return scalar_iter_bo_OPNAME(((IA_ConstDI<LTYPE> *)lhs.bdip)->value,rhs);
	}
    } else {
	if (rhs.type() == IA_VectorDI<RTYPE>::s_type()) {
	    return iter_vec_bo_OPNAME(lhs,(IA_VectorDI<RTYPE> *)rhs.bdip);
	} else if (rhs.type() == IA_ConstDI<RTYPE>::s_type()) {
	    return iter_scalar_bo_OPNAME(lhs,((IA_ConstDI<RTYPE> *)rhs.bdip)->value);
	} else {
	    return iter_iter_bo_OPNAME(lhs,rhs);
	}
    }
}
