matop.cpp 39.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009-2010, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

/* ////////////////////////////////////////////////////////////////////
//
//  Mat basic operations: Copy, Set
//
// */

#include "precomp.hpp"

namespace cv
{

class MatOp_Identity : public MatOp
{
public:
    MatOp_Identity() {}
    virtual ~MatOp_Identity() {}

60
    bool elementWise(const MatExpr& /*expr*/) const { return true; }
61
    void assign(const MatExpr& expr, Mat& m, int type=-1) const;
Andrey Kamaev's avatar
Andrey Kamaev committed
62

63 64 65 66
    static void makeExpr(MatExpr& res, const Mat& m);
};

static MatOp_Identity g_MatOp_Identity;
Andrey Kamaev's avatar
Andrey Kamaev committed
67

68 69 70 71 72
class MatOp_AddEx : public MatOp
{
public:
    MatOp_AddEx() {}
    virtual ~MatOp_AddEx() {}
Andrey Kamaev's avatar
Andrey Kamaev committed
73

74
    bool elementWise(const MatExpr& /*expr*/) const { return true; }
75
    void assign(const MatExpr& expr, Mat& m, int type=-1) const;
Andrey Kamaev's avatar
Andrey Kamaev committed
76

77 78 79 80
    void add(const MatExpr& e1, const Scalar& s, MatExpr& res) const;
    void subtract(const Scalar& s, const MatExpr& expr, MatExpr& res) const;
    void multiply(const MatExpr& e1, double s, MatExpr& res) const;
    void divide(double s, const MatExpr& e, MatExpr& res) const;
Andrey Kamaev's avatar
Andrey Kamaev committed
81

82 83
    void transpose(const MatExpr& e1, MatExpr& res) const;
    void abs(const MatExpr& expr, MatExpr& res) const;
Andrey Kamaev's avatar
Andrey Kamaev committed
84

85 86 87 88
    static void makeExpr(MatExpr& res, const Mat& a, const Mat& b, double alpha, double beta, const Scalar& s=Scalar());
};

static MatOp_AddEx g_MatOp_AddEx;
Andrey Kamaev's avatar
Andrey Kamaev committed
89

90 91 92 93 94
class MatOp_Bin : public MatOp
{
public:
    MatOp_Bin() {}
    virtual ~MatOp_Bin() {}
Andrey Kamaev's avatar
Andrey Kamaev committed
95

96
    bool elementWise(const MatExpr& /*expr*/) const { return true; }
97
    void assign(const MatExpr& expr, Mat& m, int type=-1) const;
Andrey Kamaev's avatar
Andrey Kamaev committed
98

99 100
    void multiply(const MatExpr& e1, double s, MatExpr& res) const;
    void divide(double s, const MatExpr& e, MatExpr& res) const;
Andrey Kamaev's avatar
Andrey Kamaev committed
101

102 103 104 105 106
    static void makeExpr(MatExpr& res, char op, const Mat& a, const Mat& b, double scale=1);
    static void makeExpr(MatExpr& res, char op, const Mat& a, const Scalar& s);
};

static MatOp_Bin g_MatOp_Bin;
Andrey Kamaev's avatar
Andrey Kamaev committed
107

108 109 110 111 112
class MatOp_Cmp : public MatOp
{
public:
    MatOp_Cmp() {}
    virtual ~MatOp_Cmp() {}
Andrey Kamaev's avatar
Andrey Kamaev committed
113

114
    bool elementWise(const MatExpr& /*expr*/) const { return true; }
115
    void assign(const MatExpr& expr, Mat& m, int type=-1) const;
Andrey Kamaev's avatar
Andrey Kamaev committed
116

117 118 119
    static void makeExpr(MatExpr& res, int cmpop, const Mat& a, const Mat& b);
    static void makeExpr(MatExpr& res, int cmpop, const Mat& a, double alpha);
};
Andrey Kamaev's avatar
Andrey Kamaev committed
120

121
static MatOp_Cmp g_MatOp_Cmp;
Andrey Kamaev's avatar
Andrey Kamaev committed
122

123 124 125 126 127
class MatOp_GEMM : public MatOp
{
public:
    MatOp_GEMM() {}
    virtual ~MatOp_GEMM() {}
Andrey Kamaev's avatar
Andrey Kamaev committed
128

129
    bool elementWise(const MatExpr& /*expr*/) const { return false; }
130
    void assign(const MatExpr& expr, Mat& m, int type=-1) const;
Andrey Kamaev's avatar
Andrey Kamaev committed
131

132 133 134
    void add(const MatExpr& e1, const MatExpr& e2, MatExpr& res) const;
    void subtract(const MatExpr& e1, const MatExpr& e2, MatExpr& res) const;
    void multiply(const MatExpr& e, double s, MatExpr& res) const;
Andrey Kamaev's avatar
Andrey Kamaev committed
135

136
    void transpose(const MatExpr& expr, MatExpr& res) const;
Andrey Kamaev's avatar
Andrey Kamaev committed
137

138 139 140 141 142 143 144 145 146 147 148
    static void makeExpr(MatExpr& res, int flags, const Mat& a, const Mat& b,
                         double alpha=1, const Mat& c=Mat(), double beta=1);
};

static MatOp_GEMM g_MatOp_GEMM;

class MatOp_Invert : public MatOp
{
public:
    MatOp_Invert() {}
    virtual ~MatOp_Invert() {}
Andrey Kamaev's avatar
Andrey Kamaev committed
149

150
    bool elementWise(const MatExpr& /*expr*/) const { return false; }
151
    void assign(const MatExpr& expr, Mat& m, int type=-1) const;
Andrey Kamaev's avatar
Andrey Kamaev committed
152

153
    void matmul(const MatExpr& expr1, const MatExpr& expr2, MatExpr& res) const;
Andrey Kamaev's avatar
Andrey Kamaev committed
154

155
    static void makeExpr(MatExpr& res, int method, const Mat& m);
Andrey Kamaev's avatar
Andrey Kamaev committed
156
};
157 158 159 160 161 162 163 164

static MatOp_Invert g_MatOp_Invert;

class MatOp_T : public MatOp
{
public:
    MatOp_T() {}
    virtual ~MatOp_T() {}
Andrey Kamaev's avatar
Andrey Kamaev committed
165

166
    bool elementWise(const MatExpr& /*expr*/) const { return false; }
167
    void assign(const MatExpr& expr, Mat& m, int type=-1) const;
Andrey Kamaev's avatar
Andrey Kamaev committed
168

169 170
    void multiply(const MatExpr& e1, double s, MatExpr& res) const;
    void transpose(const MatExpr& expr, MatExpr& res) const;
Andrey Kamaev's avatar
Andrey Kamaev committed
171

172 173 174 175 176 177 178 179 180 181
    static void makeExpr(MatExpr& res, const Mat& a, double alpha=1);
};

static MatOp_T g_MatOp_T;

class MatOp_Solve : public MatOp
{
public:
    MatOp_Solve() {}
    virtual ~MatOp_Solve() {}
Andrey Kamaev's avatar
Andrey Kamaev committed
182

183
    bool elementWise(const MatExpr& /*expr*/) const { return false; }
184
    void assign(const MatExpr& expr, Mat& m, int type=-1) const;
Andrey Kamaev's avatar
Andrey Kamaev committed
185

186 187 188 189 190 191 192 193 194 195
    static void makeExpr(MatExpr& res, int method, const Mat& a, const Mat& b);
};

static MatOp_Solve g_MatOp_Solve;

class MatOp_Initializer : public MatOp
{
public:
    MatOp_Initializer() {}
    virtual ~MatOp_Initializer() {}
Andrey Kamaev's avatar
Andrey Kamaev committed
196

197
    bool elementWise(const MatExpr& /*expr*/) const { return false; }
198
    void assign(const MatExpr& expr, Mat& m, int type=-1) const;
Andrey Kamaev's avatar
Andrey Kamaev committed
199

200
    void multiply(const MatExpr& e, double s, MatExpr& res) const;
Andrey Kamaev's avatar
Andrey Kamaev committed
201

202
    static void makeExpr(MatExpr& res, int method, Size sz, int type, double alpha=1);
203
    static void makeExpr(MatExpr& res, int method, int ndims, const int* sizes, int type, double alpha=1);
204 205 206
};

static MatOp_Initializer g_MatOp_Initializer;
Andrey Kamaev's avatar
Andrey Kamaev committed
207

208 209 210 211
static inline bool isIdentity(const MatExpr& e) { return e.op == &g_MatOp_Identity; }
static inline bool isAddEx(const MatExpr& e) { return e.op == &g_MatOp_AddEx; }
static inline bool isScaled(const MatExpr& e) { return isAddEx(e) && (!e.b.data || e.beta == 0) && e.s == Scalar(); }
static inline bool isBin(const MatExpr& e, char c) { return e.op == &g_MatOp_Bin && e.flags == c; }
212
static inline bool isCmp(const MatExpr& e) { return e.op == &g_MatOp_Cmp; }
213 214 215
static inline bool isReciprocal(const MatExpr& e) { return isBin(e,'/') && (!e.b.data || e.beta == 0); }
static inline bool isT(const MatExpr& e) { return e.op == &g_MatOp_T; }
static inline bool isInv(const MatExpr& e) { return e.op == &g_MatOp_Invert; }
216
static inline bool isSolve(const MatExpr& e) { return e.op == &g_MatOp_Solve; }
217 218 219
static inline bool isGEMM(const MatExpr& e) { return e.op == &g_MatOp_GEMM; }
static inline bool isMatProd(const MatExpr& e) { return e.op == &g_MatOp_GEMM && (!e.c.data || e.beta == 0); }
static inline bool isInitializer(const MatExpr& e) { return e.op == &g_MatOp_Initializer; }
Andrey Kamaev's avatar
Andrey Kamaev committed
220

221
/////////////////////////////////////////////////////////////////////////////////////////////////////
Andrey Kamaev's avatar
Andrey Kamaev committed
222

223 224 225 226
MatOp::MatOp() {}
MatOp::~MatOp() {}


227
bool MatOp::elementWise(const MatExpr& /*expr*/) const
228 229 230
{
    return false;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
231

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
void MatOp::roi(const MatExpr& expr, const Range& rowRange, const Range& colRange, MatExpr& e) const
{
    if( elementWise(expr) )
    {
        e = MatExpr(expr.op, expr.flags, Mat(), Mat(), Mat(),
                    expr.alpha, expr.beta, expr.s);
        if(expr.a.data)
            e.a = expr.a(rowRange, colRange);
        if(expr.b.data)
            e.b = expr.b(rowRange, colRange);
        if(expr.c.data)
            e.c = expr.c(rowRange, colRange);
    }
    else
    {
        Mat m;
        expr.op->assign(expr, m);
        e = MatExpr(&g_MatOp_Identity, 0, m(rowRange, colRange), Mat(), Mat());
    }
}
Andrey Kamaev's avatar
Andrey Kamaev committed
252

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
void MatOp::diag(const MatExpr& expr, int d, MatExpr& e) const
{
    if( elementWise(expr) )
    {
        e = MatExpr(expr.op, expr.flags, Mat(), Mat(), Mat(),
                    expr.alpha, expr.beta, expr.s);
        if(expr.a.data)
            e.a = expr.a.diag(d);
        if(expr.b.data)
            e.b = expr.b.diag(d);
        if(expr.c.data)
            e.c = expr.c.diag(d);
    }
    else
    {
        Mat m;
        expr.op->assign(expr, m);
        e = MatExpr(&g_MatOp_Identity, 0, m.diag(d), Mat(), Mat());
    }
}

Andrey Kamaev's avatar
Andrey Kamaev committed
274

275 276 277 278 279 280 281
void MatOp::augAssignAdd(const MatExpr& expr, Mat& m) const
{
    Mat temp;
    expr.op->assign(expr, temp);
    m += temp;
}

Andrey Kamaev's avatar
Andrey Kamaev committed
282

283 284 285 286 287 288 289
void MatOp::augAssignSubtract(const MatExpr& expr, Mat& m) const
{
    Mat temp;
    expr.op->assign(expr, temp);
    m -= temp;
}

Andrey Kamaev's avatar
Andrey Kamaev committed
290

291 292 293 294 295 296 297
void MatOp::augAssignMultiply(const MatExpr& expr, Mat& m) const
{
    Mat temp;
    expr.op->assign(expr, temp);
    m *= temp;
}

Andrey Kamaev's avatar
Andrey Kamaev committed
298

299 300 301 302 303 304
void MatOp::augAssignDivide(const MatExpr& expr, Mat& m) const
{
    Mat temp;
    expr.op->assign(expr, temp);
    m /= temp;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
305 306


307 308 309 310 311 312 313
void MatOp::augAssignAnd(const MatExpr& expr, Mat& m) const
{
    Mat temp;
    expr.op->assign(expr, temp);
    m &= temp;
}

Andrey Kamaev's avatar
Andrey Kamaev committed
314

315 316 317 318 319 320 321
void MatOp::augAssignOr(const MatExpr& expr, Mat& m) const
{
    Mat temp;
    expr.op->assign(expr, temp);
    m |= temp;
}

Andrey Kamaev's avatar
Andrey Kamaev committed
322

323 324 325 326
void MatOp::augAssignXor(const MatExpr& expr, Mat& m) const
{
    Mat temp;
    expr.op->assign(expr, temp);
327
    m ^= temp;
328
}
Andrey Kamaev's avatar
Andrey Kamaev committed
329

330

331
void MatOp::add(const MatExpr& e1, const MatExpr& e2, MatExpr& res) const
332
{
333
    if( this == e2.op )
334
    {
335 336
        double alpha = 1, beta = 1;
        Scalar s;
337
        Mat m1, m2;
338 339 340 341 342 343 344 345
        if( isAddEx(e1) && (!e1.b.data || e1.beta == 0) )
        {
            m1 = e1.a;
            alpha = e1.alpha;
            s = e1.s;
        }
        else
            e1.op->assign(e1, m1);
Andrey Kamaev's avatar
Andrey Kamaev committed
346

347 348 349 350 351
        if( isAddEx(e2) && (!e2.b.data || e2.beta == 0) )
        {
            m2 = e2.a;
            beta = e2.alpha;
            s += e2.s;
Andrey Kamaev's avatar
Andrey Kamaev committed
352
        }
353 354 355
        else
            e2.op->assign(e2, m2);
        MatOp_AddEx::makeExpr(res, m1, m2, alpha, beta, s);
356 357
    }
    else
358
        e2.op->add(e1, e2, res);
359 360
}

Andrey Kamaev's avatar
Andrey Kamaev committed
361

362 363 364 365 366 367 368
void MatOp::add(const MatExpr& expr1, const Scalar& s, MatExpr& res) const
{
    Mat m1;
    expr1.op->assign(expr1, m1);
    MatOp_AddEx::makeExpr(res, m1, Mat(), 1, 0, s);
}

Andrey Kamaev's avatar
Andrey Kamaev committed
369

370
void MatOp::subtract(const MatExpr& e1, const MatExpr& e2, MatExpr& res) const
371
{
372
    if( this == e2.op )
373
    {
374 375
        double alpha = 1, beta = -1;
        Scalar s;
376
        Mat m1, m2;
377 378 379 380 381 382 383 384
        if( isAddEx(e1) && (!e1.b.data || e1.beta == 0) )
        {
            m1 = e1.a;
            alpha = e1.alpha;
            s = e1.s;
        }
        else
            e1.op->assign(e1, m1);
Andrey Kamaev's avatar
Andrey Kamaev committed
385

386 387 388 389 390
        if( isAddEx(e2) && (!e2.b.data || e2.beta == 0) )
        {
            m2 = e2.a;
            beta = -e2.alpha;
            s -= e2.s;
Andrey Kamaev's avatar
Andrey Kamaev committed
391
        }
392 393 394
        else
            e2.op->assign(e2, m2);
        MatOp_AddEx::makeExpr(res, m1, m2, alpha, beta, s);
395 396
    }
    else
397
        e2.op->subtract(e1, e2, res);
398 399
}

Andrey Kamaev's avatar
Andrey Kamaev committed
400

401 402 403 404 405 406 407
void MatOp::subtract(const Scalar& s, const MatExpr& expr, MatExpr& res) const
{
    Mat m;
    expr.op->assign(expr, m);
    MatOp_AddEx::makeExpr(res, m, Mat(), -1, 0, s);
}

Andrey Kamaev's avatar
Andrey Kamaev committed
408

409
void MatOp::multiply(const MatExpr& e1, const MatExpr& e2, MatExpr& res, double scale) const
410
{
411
    if( this == e2.op )
412 413
    {
        Mat m1, m2;
Andrey Kamaev's avatar
Andrey Kamaev committed
414

415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
        if( isReciprocal(e1) )
        {
            if( isScaled(e2) )
            {
                scale *= e2.alpha;
                m2 = e2.a;
            }
            else
                e2.op->assign(e2, m2);

            MatOp_Bin::makeExpr(res, '/', m2, e1.a, scale/e1.alpha);
        }
        else
        {
            char op = '*';
            if( isScaled(e1) )
            {
                m1 = e1.a;
                scale *= e1.alpha;
            }
            else
                e1.op->assign(e1, m1);
Andrey Kamaev's avatar
Andrey Kamaev committed
437

438 439 440 441 442 443 444 445 446 447 448 449 450
            if( isScaled(e2) )
            {
                m2 = e2.a;
                scale *= e2.alpha;
            }
            else if( isReciprocal(e2) )
            {
                op = '/';
                m2 = e2.a;
                scale /= e2.alpha;
            }
            else
                e2.op->assign(e2, m2);
Andrey Kamaev's avatar
Andrey Kamaev committed
451

452 453
            MatOp_Bin::makeExpr(res, op, m1, m2, scale);
        }
454 455
    }
    else
456
        e2.op->multiply(e1, e2, res, scale);
457
}
Andrey Kamaev's avatar
Andrey Kamaev committed
458 459


460 461 462 463
void MatOp::multiply(const MatExpr& expr, double s, MatExpr& res) const
{
    Mat m;
    expr.op->assign(expr, m);
Andrey Kamaev's avatar
Andrey Kamaev committed
464
    MatOp_AddEx::makeExpr(res, m, Mat(), s, 0);
465
}
Andrey Kamaev's avatar
Andrey Kamaev committed
466 467


468
void MatOp::divide(const MatExpr& e1, const MatExpr& e2, MatExpr& res, double scale) const
469
{
470
    if( this == e2.op )
471
    {
472 473 474 475 476 477
        if( isReciprocal(e1) && isReciprocal(e2) )
            MatOp_Bin::makeExpr(res, '/', e2.a, e1.a, e1.alpha/e2.alpha);
        else
        {
            Mat m1, m2;
            char op = '/';
Andrey Kamaev's avatar
Andrey Kamaev committed
478

479 480 481 482 483 484 485
            if( isScaled(e1) )
            {
                m1 = e1.a;
                scale *= e1.alpha;
            }
            else
                e1.op->assign(e1, m1);
Andrey Kamaev's avatar
Andrey Kamaev committed
486

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
            if( isScaled(e2) )
            {
                m2 = e2.a;
                scale /= e2.alpha;
            }
            else if( isReciprocal(e2) )
            {
                m2 = e2.a;
                scale /= e2.alpha;
                op = '*';
            }
            else
                e2.op->assign(e2, m2);
            MatOp_Bin::makeExpr(res, op, m1, m2, scale);
        }
502 503
    }
    else
504
        e2.op->divide(e1, e2, res, scale);
505 506
}

Andrey Kamaev's avatar
Andrey Kamaev committed
507

508 509 510 511 512 513 514
void MatOp::divide(double s, const MatExpr& expr, MatExpr& res) const
{
    Mat m;
    expr.op->assign(expr, m);
    MatOp_Bin::makeExpr(res, '/', m, Mat(), s);
}

Andrey Kamaev's avatar
Andrey Kamaev committed
515

516 517 518 519 520 521 522
void MatOp::abs(const MatExpr& expr, MatExpr& res) const
{
    Mat m;
    expr.op->assign(expr, m);
    MatOp_Bin::makeExpr(res, 'a', m, Mat());
}

Andrey Kamaev's avatar
Andrey Kamaev committed
523

524 525 526 527 528 529 530
void MatOp::transpose(const MatExpr& expr, MatExpr& res) const
{
    Mat m;
    expr.op->assign(expr, m);
    MatOp_T::makeExpr(res, m, 1);
}

Andrey Kamaev's avatar
Andrey Kamaev committed
531

532
void MatOp::matmul(const MatExpr& e1, const MatExpr& e2, MatExpr& res) const
533
{
534
    if( this == e2.op )
535
    {
536 537
        double scale = 1;
        int flags = 0;
538
        Mat m1, m2;
Andrey Kamaev's avatar
Andrey Kamaev committed
539

540 541 542 543 544 545 546 547 548 549 550 551 552
        if( isT(e1) )
        {
            flags = CV_GEMM_A_T;
            scale = e1.alpha;
            m1 = e1.a;
        }
        else if( isScaled(e1) )
        {
            scale = e1.alpha;
            m1 = e1.a;
        }
        else
            e1.op->assign(e1, m1);
Andrey Kamaev's avatar
Andrey Kamaev committed
553

554 555 556 557 558 559 560 561 562 563 564 565 566
        if( isT(e2) )
        {
            flags |= CV_GEMM_B_T;
            scale *= e2.alpha;
            m2 = e2.a;
        }
        else if( isScaled(e2) )
        {
            scale *= e2.alpha;
            m2 = e2.a;
        }
        else
            e2.op->assign(e2, m2);
Andrey Kamaev's avatar
Andrey Kamaev committed
567

568
        MatOp_GEMM::makeExpr(res, flags, m1, m2, scale);
569 570
    }
    else
571
        e2.op->matmul(e1, e2, res);
572 573
}

Andrey Kamaev's avatar
Andrey Kamaev committed
574

575 576 577 578 579 580
void MatOp::invert(const MatExpr& expr, int method, MatExpr& res) const
{
    Mat m;
    expr.op->assign(expr, m);
    MatOp_Invert::makeExpr(res, method, m);
}
Andrey Kamaev's avatar
Andrey Kamaev committed
581 582


583 584 585 586
Size MatOp::size(const MatExpr& expr) const
{
    return !expr.a.empty() ? expr.a.size() : expr.b.empty() ? expr.b.size() : expr.c.size();
}
587

588 589 590
int MatOp::type(const MatExpr& expr) const
{
    return !expr.a.empty() ? expr.a.type() : expr.b.empty() ? expr.b.type() : expr.c.type();
Andrey Kamaev's avatar
Andrey Kamaev committed
591 592
}

593 594 595 596 597
//////////////////////////////////////////////////////////////////////////////////////////////////

MatExpr::MatExpr(const Mat& m) : op(&g_MatOp_Identity), flags(0), a(m), b(Mat()), c(Mat()), alpha(1), beta(0), s(Scalar())
{
}
Andrey Kamaev's avatar
Andrey Kamaev committed
598

599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
MatExpr MatExpr::row(int y) const
{
    MatExpr e;
    op->roi(*this, Range(y, y+1), Range::all(), e);
    return e;
}

MatExpr MatExpr::col(int x) const
{
    MatExpr e;
    op->roi(*this, Range::all(), Range(x, x+1), e);
    return e;
}

MatExpr MatExpr::diag(int d) const
{
    MatExpr e;
    op->diag(*this, d, e);
    return e;
}

MatExpr MatExpr::operator()( const Range& rowRange, const Range& colRange ) const
{
    MatExpr e;
    op->roi(*this, rowRange, colRange, e);
    return e;
}

MatExpr MatExpr::operator()( const Rect& roi ) const
{
    MatExpr e;
    op->roi(*this, Range(roi.y, roi.y + roi.height), Range(roi.x, roi.x + roi.width), e);
    return e;
}

Mat MatExpr::cross(const Mat& m) const
{
    return ((Mat)*this).cross(m);
}

double MatExpr::dot(const Mat& m) const
{
    return ((Mat)*this).dot(m);
}

MatExpr MatExpr::t() const
{
    MatExpr e;
    op->transpose(*this, e);
    return e;
}

MatExpr MatExpr::inv(int method) const
{
    MatExpr e;
    op->invert(*this, method, e);
    return e;
}

MatExpr MatExpr::mul(const MatExpr& e, double scale) const
{
    MatExpr en;
    op->multiply(*this, e, en, scale);
    return en;
}

MatExpr MatExpr::mul(const Mat& m, double scale) const
{
    MatExpr e;
    op->multiply(*this, MatExpr(m), e, scale);
    return e;
}

MatExpr operator + (const Mat& a, const Mat& b)
{
    MatExpr e;
    MatOp_AddEx::makeExpr(e, a, b, 1, 1);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
678

679 680 681 682 683 684
MatExpr operator + (const Mat& a, const Scalar& s)
{
    MatExpr e;
    MatOp_AddEx::makeExpr(e, a, Mat(), 1, 0, s);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
685

686 687 688 689 690
MatExpr operator + (const Scalar& s, const Mat& a)
{
    MatExpr e;
    MatOp_AddEx::makeExpr(e, a, Mat(), 1, 0, s);
    return e;
Andrey Kamaev's avatar
Andrey Kamaev committed
691 692
}

693 694 695 696 697 698
MatExpr operator + (const MatExpr& e, const Mat& m)
{
    MatExpr en;
    e.op->add(e, MatExpr(m), en);
    return en;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
699

700 701 702 703 704
MatExpr operator + (const Mat& m, const MatExpr& e)
{
    MatExpr en;
    e.op->add(e, MatExpr(m), en);
    return en;
Andrey Kamaev's avatar
Andrey Kamaev committed
705 706
}

707 708 709 710 711 712
MatExpr operator + (const MatExpr& e, const Scalar& s)
{
    MatExpr en;
    e.op->add(e, s, en);
    return en;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
713

714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
MatExpr operator + (const Scalar& s, const MatExpr& e)
{
    MatExpr en;
    e.op->add(e, s, en);
    return en;
}

MatExpr operator + (const MatExpr& e1, const MatExpr& e2)
{
    MatExpr en;
    e1.op->add(e1, e2, en);
    return en;
}

MatExpr operator - (const Mat& a, const Mat& b)
{
    MatExpr e;
    MatOp_AddEx::makeExpr(e, a, b, 1, -1);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
734

735 736 737 738 739 740
MatExpr operator - (const Mat& a, const Scalar& s)
{
    MatExpr e;
    MatOp_AddEx::makeExpr(e, a, Mat(), 1, 0, -s);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
741

742 743 744 745 746 747
MatExpr operator - (const Scalar& s, const Mat& a)
{
    MatExpr e;
    MatOp_AddEx::makeExpr(e, a, Mat(), -1, 0, s);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
748

749 750 751 752 753 754
MatExpr operator - (const MatExpr& e, const Mat& m)
{
    MatExpr en;
    e.op->subtract(e, MatExpr(m), en);
    return en;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
755

756 757 758 759 760 761
MatExpr operator - (const Mat& m, const MatExpr& e)
{
    MatExpr en;
    e.op->subtract(MatExpr(m), e, en);
    return en;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
762

763 764 765 766 767 768
MatExpr operator - (const MatExpr& e, const Scalar& s)
{
    MatExpr en;
    e.op->add(e, -s, en);
    return en;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
769

770 771 772 773 774 775
MatExpr operator - (const Scalar& s, const MatExpr& e)
{
    MatExpr en;
    e.op->subtract(s, e, en);
    return en;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
776

777 778 779 780 781 782 783 784 785 786 787 788 789
MatExpr operator - (const MatExpr& e1, const MatExpr& e2)
{
    MatExpr en;
    e1.op->subtract(e1, e2, en);
    return en;
}

MatExpr operator - (const Mat& m)
{
    MatExpr e;
    MatOp_AddEx::makeExpr(e, m, Mat(), -1, 0);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
790

791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
MatExpr operator - (const MatExpr& e)
{
    MatExpr en;
    e.op->subtract(Scalar(0), e, en);
    return en;
}

MatExpr operator * (const Mat& a, const Mat& b)
{
    MatExpr e;
    MatOp_GEMM::makeExpr(e, 0, a, b);
    return e;
}

MatExpr operator * (const Mat& a, double s)
{
    MatExpr e;
    MatOp_AddEx::makeExpr(e, a, Mat(), s, 0);
    return e;
}

MatExpr operator * (double s, const Mat& a)
{
    MatExpr e;
    MatOp_AddEx::makeExpr(e, a, Mat(), s, 0);
    return e;
}

MatExpr operator * (const MatExpr& e, const Mat& m)
{
    MatExpr en;
    e.op->matmul(e, MatExpr(m), en);
    return en;
}

MatExpr operator * (const Mat& m, const MatExpr& e)
{
    MatExpr en;
    e.op->matmul(MatExpr(m), e, en);
    return en;
}

MatExpr operator * (const MatExpr& e, double s)
{
    MatExpr en;
    e.op->multiply(e, s, en);
    return en;
}

MatExpr operator * (double s, const MatExpr& e)
{
    MatExpr en;
    e.op->multiply(e, s, en);
    return en;
}

MatExpr operator * (const MatExpr& e1, const MatExpr& e2)
{
    MatExpr en;
    e1.op->matmul(e1, e2, en);
    return en;
}

MatExpr operator / (const Mat& a, const Mat& b)
{
    MatExpr e;
    MatOp_Bin::makeExpr(e, '/', a, b);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
860

861 862 863 864 865 866
MatExpr operator / (const Mat& a, double s)
{
    MatExpr e;
    MatOp_AddEx::makeExpr(e, a, Mat(), 1./s, 0);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
867

868 869 870 871 872 873
MatExpr operator / (double s, const Mat& a)
{
    MatExpr e;
    MatOp_Bin::makeExpr(e, '/', a, Mat(), s);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
874

875 876 877 878 879 880
MatExpr operator / (const MatExpr& e, const Mat& m)
{
    MatExpr en;
    e.op->divide(e, MatExpr(m), en);
    return en;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
881

882 883 884 885 886 887
MatExpr operator / (const Mat& m, const MatExpr& e)
{
    MatExpr en;
    e.op->divide(MatExpr(m), e, en);
    return en;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
888

889 890 891 892 893 894
MatExpr operator / (const MatExpr& e, double s)
{
    MatExpr en;
    e.op->multiply(e, 1./s, en);
    return en;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
895

896 897 898 899 900 901
MatExpr operator / (double s, const MatExpr& e)
{
    MatExpr en;
    e.op->divide(s, e, en);
    return en;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
902

903 904 905 906 907 908 909 910 911 912 913 914 915
MatExpr operator / (const MatExpr& e1, const MatExpr& e2)
{
    MatExpr en;
    e1.op->divide(e1, e2, en);
    return en;
}

MatExpr operator < (const Mat& a, const Mat& b)
{
    MatExpr e;
    MatOp_Cmp::makeExpr(e, CV_CMP_LT, a, b);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
916

917 918 919 920 921 922
MatExpr operator < (const Mat& a, double s)
{
    MatExpr e;
    MatOp_Cmp::makeExpr(e, CV_CMP_LT, a, s);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
923

924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
MatExpr operator < (double s, const Mat& a)
{
    MatExpr e;
    MatOp_Cmp::makeExpr(e, CV_CMP_GT, a, s);
    return e;
}

MatExpr operator <= (const Mat& a, const Mat& b)
{
    MatExpr e;
    MatOp_Cmp::makeExpr(e, CV_CMP_LE, a, b);
    return e;
}

MatExpr operator <= (const Mat& a, double s)
{
    MatExpr e;
    MatOp_Cmp::makeExpr(e, CV_CMP_LE, a, s);
    return e;
}

MatExpr operator <= (double s, const Mat& a)
{
    MatExpr e;
    MatOp_Cmp::makeExpr(e, CV_CMP_GE, a, s);
    return e;
}

MatExpr operator == (const Mat& a, const Mat& b)
{
    MatExpr e;
    MatOp_Cmp::makeExpr(e, CV_CMP_EQ, a, b);
    return e;
}

MatExpr operator == (const Mat& a, double s)
{
    MatExpr e;
    MatOp_Cmp::makeExpr(e, CV_CMP_EQ, a, s);
    return e;
}

MatExpr operator == (double s, const Mat& a)
{
    MatExpr e;
    MatOp_Cmp::makeExpr(e, CV_CMP_EQ, a, s);
    return e;
}

MatExpr operator != (const Mat& a, const Mat& b)
{
    MatExpr e;
    MatOp_Cmp::makeExpr(e, CV_CMP_NE, a, b);
    return e;
}

MatExpr operator != (const Mat& a, double s)
{
    MatExpr e;
    MatOp_Cmp::makeExpr(e, CV_CMP_NE, a, s);
    return e;
}

MatExpr operator != (double s, const Mat& a)
{
    MatExpr e;
    MatOp_Cmp::makeExpr(e, CV_CMP_NE, a, s);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
993

994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
MatExpr operator >= (const Mat& a, const Mat& b)
{
    MatExpr e;
    MatOp_Cmp::makeExpr(e, CV_CMP_GE, a, b);
    return e;
}

MatExpr operator >= (const Mat& a, double s)
{
    MatExpr e;
    MatOp_Cmp::makeExpr(e, CV_CMP_GE, a, s);
    return e;
}

MatExpr operator >= (double s, const Mat& a)
{
    MatExpr e;
    MatOp_Cmp::makeExpr(e, CV_CMP_LE, a, s);
    return e;
}

MatExpr operator > (const Mat& a, const Mat& b)
{
    MatExpr e;
    MatOp_Cmp::makeExpr(e, CV_CMP_GT, a, b);
    return e;
}

MatExpr operator > (const Mat& a, double s)
{
    MatExpr e;
    MatOp_Cmp::makeExpr(e, CV_CMP_GT, a, s);
    return e;
}

MatExpr operator > (double s, const Mat& a)
{
    MatExpr e;
    MatOp_Cmp::makeExpr(e, CV_CMP_LT, a, s);
    return e;
Andrey Kamaev's avatar
Andrey Kamaev committed
1034 1035
}

1036 1037 1038 1039 1040 1041
MatExpr min(const Mat& a, const Mat& b)
{
    MatExpr e;
    MatOp_Bin::makeExpr(e, 'm', a, b);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1042

1043 1044 1045 1046 1047 1048
MatExpr min(const Mat& a, double s)
{
    MatExpr e;
    MatOp_Bin::makeExpr(e, 'm', a, s);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1049

1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
MatExpr min(double s, const Mat& a)
{
    MatExpr e;
    MatOp_Bin::makeExpr(e, 'm', a, s);
    return e;
}

MatExpr max(const Mat& a, const Mat& b)
{
    MatExpr e;
    MatOp_Bin::makeExpr(e, 'M', a, b);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1063

1064 1065 1066 1067 1068 1069
MatExpr max(const Mat& a, double s)
{
    MatExpr e;
    MatOp_Bin::makeExpr(e, 'M', a, s);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1070

1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
MatExpr max(double s, const Mat& a)
{
    MatExpr e;
    MatOp_Bin::makeExpr(e, 'M', a, s);
    return e;
}

MatExpr operator & (const Mat& a, const Mat& b)
{
    MatExpr e;
    MatOp_Bin::makeExpr(e, '&', a, b);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1084

1085 1086 1087 1088 1089 1090
MatExpr operator & (const Mat& a, const Scalar& s)
{
    MatExpr e;
    MatOp_Bin::makeExpr(e, '&', a, s);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1091

1092 1093 1094 1095 1096 1097
MatExpr operator & (const Scalar& s, const Mat& a)
{
    MatExpr e;
    MatOp_Bin::makeExpr(e, '&', a, s);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1098

1099 1100 1101 1102 1103 1104
MatExpr operator | (const Mat& a, const Mat& b)
{
    MatExpr e;
    MatOp_Bin::makeExpr(e, '|', a, b);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1105

1106 1107 1108 1109 1110 1111
MatExpr operator | (const Mat& a, const Scalar& s)
{
    MatExpr e;
    MatOp_Bin::makeExpr(e, '|', a, s);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1112

1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
MatExpr operator | (const Scalar& s, const Mat& a)
{
    MatExpr e;
    MatOp_Bin::makeExpr(e, '|', a, s);
    return e;
}

MatExpr operator ^ (const Mat& a, const Mat& b)
{
    MatExpr e;
    MatOp_Bin::makeExpr(e, '^', a, b);
    return e;
}

MatExpr operator ^ (const Mat& a, const Scalar& s)
{
    MatExpr e;
    MatOp_Bin::makeExpr(e, '^', a, s);
    return e;
}

MatExpr operator ^ (const Scalar& s, const Mat& a)
{
    MatExpr e;
    MatOp_Bin::makeExpr(e, '^', a, s);
    return e;
}

MatExpr operator ~(const Mat& a)
{
    MatExpr e;
    MatOp_Bin::makeExpr(e, '~', a, Scalar());
    return e;
}

MatExpr abs(const Mat& a)
{
    MatExpr e;
    MatOp_Bin::makeExpr(e, 'a', a, Scalar());
    return e;
}

MatExpr abs(const MatExpr& e)
{
    MatExpr en;
    e.op->abs(e, en);
    return en;
}

Andrey Kamaev's avatar
Andrey Kamaev committed
1162

1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
Size MatExpr::size() const
{
    if( isT(*this) || isInv(*this) )
        return Size(a.rows, a.cols);
    if( isGEMM(*this) )
        return Size(b.cols, a.rows);
    if( isSolve(*this) )
        return Size(b.cols, a.cols);
    if( isInitializer(*this) )
        return a.size();
    return op ? op->size(*this) : Size();
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1175 1176


1177 1178 1179 1180 1181 1182 1183 1184
int MatExpr::type() const
{
    if( isInitializer(*this) )
        return a.type();
    if( isCmp(*this) )
        return CV_8U;
    return op ? op->type(*this) : -1;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1185 1186


1187
/////////////////////////////////////////////////////////////////////////////////////////////////////
Andrey Kamaev's avatar
Andrey Kamaev committed
1188 1189

void MatOp_Identity::assign(const MatExpr& e, Mat& m, int _type) const
1190
{
Andrey Kamaev's avatar
Andrey Kamaev committed
1191
    if( _type == -1 || _type == e.a.type() )
1192 1193 1194
        m = e.a;
    else
    {
Andrey Kamaev's avatar
Andrey Kamaev committed
1195 1196
        CV_Assert( CV_MAT_CN(_type) == e.a.channels() );
        e.a.convertTo(m, _type);
1197 1198 1199
    }
}

1200
inline void MatOp_Identity::makeExpr(MatExpr& res, const Mat& m)
1201
{
1202
    res = MatExpr(&g_MatOp_Identity, 0, m, Mat(), Mat(), 1, 0);
Andrey Kamaev's avatar
Andrey Kamaev committed
1203 1204
}

1205 1206
/////////////////////////////////////////////////////////////////////////////////////////////////////

Andrey Kamaev's avatar
Andrey Kamaev committed
1207 1208 1209
void MatOp_AddEx::assign(const MatExpr& e, Mat& m, int _type) const
{
    Mat temp, &dst = _type == -1 || e.a.type() == _type ? m : temp;
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
    if( e.b.data )
    {
        if( e.s == Scalar() || !e.s.isReal() )
        {
            if( e.alpha == 1 )
            {
                if( e.beta == 1 )
                    cv::add(e.a, e.b, dst);
                else if( e.beta == -1 )
                    cv::subtract(e.a, e.b, dst);
                else
                    cv::scaleAdd(e.b, e.beta, e.a, dst);
            }
            else if( e.beta == 1 )
            {
                if( e.alpha == -1 )
                    cv::subtract(e.b, e.a, dst);
                else
                    cv::scaleAdd(e.a, e.alpha, e.b, dst);
            }
            else
                cv::addWeighted(e.a, e.alpha, e.b, e.beta, 0, dst);
Andrey Kamaev's avatar
Andrey Kamaev committed
1232

1233 1234 1235 1236 1237 1238 1239 1240
            if( !e.s.isReal() )
                cv::add(dst, e.s, dst);
        }
        else
            cv::addWeighted(e.a, e.alpha, e.b, e.beta, e.s[0], dst);
    }
    else if( e.s.isReal() && (dst.data != m.data || fabs(e.alpha) != 1))
    {
Andrey Kamaev's avatar
Andrey Kamaev committed
1241
        e.a.convertTo(m, _type, e.alpha, e.s[0]);
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
        return;
    }
    else if( e.alpha == 1 )
        cv::add(e.a, e.s, dst);
    else if( e.alpha == -1 )
        cv::subtract(e.s, e.a, dst);
    else
    {
        e.a.convertTo(dst, e.a.type(), e.alpha);
        cv::add(dst, e.s, dst);
    }
Andrey Kamaev's avatar
Andrey Kamaev committed
1253

1254 1255 1256 1257
    if( dst.data != m.data )
        dst.convertTo(m, m.type());
}

Andrey Kamaev's avatar
Andrey Kamaev committed
1258

1259 1260 1261 1262 1263 1264
void MatOp_AddEx::add(const MatExpr& e, const Scalar& s, MatExpr& res) const
{
    res = e;
    res.s += s;
}

Andrey Kamaev's avatar
Andrey Kamaev committed
1265

1266 1267 1268 1269 1270 1271 1272
void MatOp_AddEx::subtract(const Scalar& s, const MatExpr& e, MatExpr& res) const
{
    res = e;
    res.alpha = -res.alpha;
    res.beta = -res.beta;
    res.s = s - res.s;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1273

1274 1275 1276 1277 1278 1279 1280
void MatOp_AddEx::multiply(const MatExpr& e, double s, MatExpr& res) const
{
    res = e;
    res.alpha *= s;
    res.beta *= s;
    res.s *= s;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1281

1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
void MatOp_AddEx::divide(double s, const MatExpr& e, MatExpr& res) const
{
    if( isScaled(e) )
        MatOp_Bin::makeExpr(res, '/', e.a, Mat(), s/e.alpha);
    else
        MatOp::divide(s, e, res);
}


void MatOp_AddEx::transpose(const MatExpr& e, MatExpr& res) const
{
    if( isScaled(e) )
        MatOp_T::makeExpr(res, e.a, e.alpha);
    else
        MatOp::transpose(e, res);
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1298

1299 1300 1301 1302 1303 1304 1305 1306 1307
void MatOp_AddEx::abs(const MatExpr& e, MatExpr& res) const
{
    if( (!e.b.data || e.beta == 0) && fabs(e.alpha) == 1 )
        MatOp_Bin::makeExpr(res, 'a', e.a, -e.s*e.alpha);
    else if( e.b.data && e.alpha + e.beta == 0 && e.alpha*e.beta == -1 )
        MatOp_Bin::makeExpr(res, 'a', e.a, e.b);
    else
        MatOp::abs(e, res);
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1308

1309 1310 1311 1312
inline void MatOp_AddEx::makeExpr(MatExpr& res, const Mat& a, const Mat& b, double alpha, double beta, const Scalar& s)
{
    res = MatExpr(&g_MatOp_AddEx, 0, a, b, Mat(), alpha, beta, s);
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1313

1314
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Andrey Kamaev's avatar
Andrey Kamaev committed
1315 1316

void MatOp_Bin::assign(const MatExpr& e, Mat& m, int _type) const
1317
{
Andrey Kamaev's avatar
Andrey Kamaev committed
1318 1319
    Mat temp, &dst = _type == -1 || e.a.type() == _type ? m : temp;

1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
    if( e.flags == '*' )
        cv::multiply(e.a, e.b, dst, e.alpha);
    else if( e.flags == '/' && e.b.data )
        cv::divide(e.a, e.b, dst, e.alpha);
    else if( e.flags == '/' && !e.b.data )
        cv::divide(e.alpha, e.a, dst );
    else if( e.flags == '&' && e.b.data )
        bitwise_and(e.a, e.b, dst);
    else if( e.flags == '&' && !e.b.data )
        bitwise_and(e.a, e.s, dst);
    else if( e.flags == '|' && e.b.data )
        bitwise_or(e.a, e.b, dst);
    else if( e.flags == '|' && !e.b.data )
        bitwise_or(e.a, e.s, dst);
    else if( e.flags == '^' && e.b.data )
        bitwise_xor(e.a, e.b, dst);
    else if( e.flags == '^' && !e.b.data )
        bitwise_xor(e.a, e.s, dst);
    else if( e.flags == '~' && !e.b.data )
        bitwise_not(e.a, dst);
    else if( e.flags == 'm' && e.b.data )
        cv::min(e.a, e.b, dst);
    else if( e.flags == 'm' && !e.b.data )
        cv::min(e.a, e.s[0], dst);
    else if( e.flags == 'M' && e.b.data )
        cv::max(e.a, e.b, dst);
    else if( e.flags == 'M' && !e.b.data )
        cv::max(e.a, e.s[0], dst);
    else if( e.flags == 'a' && e.b.data )
        cv::absdiff(e.a, e.b, dst);
    else if( e.flags == 'a' && !e.b.data )
        cv::absdiff(e.a, e.s, dst);
    else
        CV_Error(CV_StsError, "Unknown operation");
Andrey Kamaev's avatar
Andrey Kamaev committed
1354

1355
    if( dst.data != m.data )
Andrey Kamaev's avatar
Andrey Kamaev committed
1356
        dst.convertTo(m, _type);
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
}

void MatOp_Bin::multiply(const MatExpr& e, double s, MatExpr& res) const
{
    if( e.flags == '*' || e.flags == '/' )
    {
        res = e;
        res.alpha *= s;
    }
    else
        MatOp::multiply(e, s, res);
}

void MatOp_Bin::divide(double s, const MatExpr& e, MatExpr& res) const
{
1372
    if( e.flags == '/' && (!e.b.data || e.beta == 0) )
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
        MatOp_AddEx::makeExpr(res, e.a, Mat(), s/e.alpha, 0);
    else
        MatOp::divide(s, e, res);
}

inline void MatOp_Bin::makeExpr(MatExpr& res, char op, const Mat& a, const Mat& b, double scale)
{
    res = MatExpr(&g_MatOp_Bin, op, a, b, Mat(), scale, b.data ? 1 : 0);
}

inline void MatOp_Bin::makeExpr(MatExpr& res, char op, const Mat& a, const Scalar& s)
{
    res = MatExpr(&g_MatOp_Bin, op, a, Mat(), Mat(), 1, 0, s);
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1387

1388
///////////////////////////////////////////////////////////////////////////////////////////////////////
Andrey Kamaev's avatar
Andrey Kamaev committed
1389 1390

void MatOp_Cmp::assign(const MatExpr& e, Mat& m, int _type) const
1391
{
Andrey Kamaev's avatar
Andrey Kamaev committed
1392 1393
    Mat temp, &dst = _type == -1 || _type == CV_8U ? m : temp;

1394 1395 1396 1397
    if( e.b.data )
        cv::compare(e.a, e.b, dst, e.flags);
    else
        cv::compare(e.a, e.alpha, dst, e.flags);
Andrey Kamaev's avatar
Andrey Kamaev committed
1398

1399
    if( dst.data != m.data )
Andrey Kamaev's avatar
Andrey Kamaev committed
1400
        dst.convertTo(m, _type);
1401 1402 1403 1404 1405 1406
}

inline void MatOp_Cmp::makeExpr(MatExpr& res, int cmpop, const Mat& a, const Mat& b)
{
    res = MatExpr(&g_MatOp_Cmp, cmpop, a, b, Mat(), 1, 1);
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1407

1408 1409 1410 1411 1412 1413
inline void MatOp_Cmp::makeExpr(MatExpr& res, int cmpop, const Mat& a, double alpha)
{
    res = MatExpr(&g_MatOp_Cmp, cmpop, a, Mat(), Mat(), alpha, 1);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////
Andrey Kamaev's avatar
Andrey Kamaev committed
1414 1415

void MatOp_T::assign(const MatExpr& e, Mat& m, int _type) const
1416
{
Andrey Kamaev's avatar
Andrey Kamaev committed
1417 1418
    Mat temp, &dst = _type == -1 || _type == e.a.type() ? m : temp;

1419
    cv::transpose(e.a, dst);
Andrey Kamaev's avatar
Andrey Kamaev committed
1420

1421
    if( dst.data != m.data || e.alpha != 1 )
Andrey Kamaev's avatar
Andrey Kamaev committed
1422
        dst.convertTo(m, _type, e.alpha);
1423 1424 1425 1426 1427 1428 1429
}

void MatOp_T::multiply(const MatExpr& e, double s, MatExpr& res) const
{
    res = e;
    res.alpha *= s;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1430

1431 1432 1433 1434 1435 1436 1437
void MatOp_T::transpose(const MatExpr& e, MatExpr& res) const
{
    if( e.alpha == 1 )
        MatOp_Identity::makeExpr(res, e.a);
    else
        MatOp_AddEx::makeExpr(res, e.a, Mat(), e.alpha, 0);
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1438

1439 1440 1441 1442 1443 1444
inline void MatOp_T::makeExpr(MatExpr& res, const Mat& a, double alpha)
{
    res = MatExpr(&g_MatOp_T, 0, a, Mat(), Mat(), alpha, 0);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////
Andrey Kamaev's avatar
Andrey Kamaev committed
1445 1446

void MatOp_GEMM::assign(const MatExpr& e, Mat& m, int _type) const
1447
{
Andrey Kamaev's avatar
Andrey Kamaev committed
1448 1449
    Mat temp, &dst = _type == -1 || _type == e.a.type() ? m : temp;

1450 1451
    cv::gemm(e.a, e.b, e.alpha, e.c, e.beta, dst, e.flags);
    if( dst.data != m.data )
Andrey Kamaev's avatar
Andrey Kamaev committed
1452
        dst.convertTo(m, _type);
1453 1454 1455 1456 1457 1458
}

void MatOp_GEMM::add(const MatExpr& e1, const MatExpr& e2, MatExpr& res) const
{
    bool i1 = isIdentity(e1), i2 = isIdentity(e2);
    double alpha1 = i1 ? 1 : e1.alpha, alpha2 = i2 ? 1 : e2.alpha;
Andrey Kamaev's avatar
Andrey Kamaev committed
1459

1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
    if( isMatProd(e1) && (i2 || isScaled(e2) || isT(e2)) )
        MatOp_GEMM::makeExpr(res, (e1.flags & ~CV_GEMM_C_T)|(isT(e2) ? CV_GEMM_C_T : 0),
                             e1.a, e1.b, alpha1, e2.a, alpha2);
    else if( isMatProd(e2) && (i1 || isScaled(e1) || isT(e1)) )
        MatOp_GEMM::makeExpr(res, (e2.flags & ~CV_GEMM_C_T)|(isT(e1) ? CV_GEMM_C_T : 0),
                             e2.a, e2.b, alpha2, e1.a, alpha1);
    else if( this == e2.op )
        MatOp::add(e1, e2, res);
    else
        e2.op->add(e1, e2, res);
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1471

1472 1473 1474 1475
void MatOp_GEMM::subtract(const MatExpr& e1, const MatExpr& e2, MatExpr& res) const
{
    bool i1 = isIdentity(e1), i2 = isIdentity(e2);
    double alpha1 = i1 ? 1 : e1.alpha, alpha2 = i2 ? 1 : e2.alpha;
Andrey Kamaev's avatar
Andrey Kamaev committed
1476

1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
    if( isMatProd(e1) && (i2 || isScaled(e2) || isT(e2)) )
        MatOp_GEMM::makeExpr(res, (e1.flags & ~CV_GEMM_C_T)|(isT(e2) ? CV_GEMM_C_T : 0),
                             e1.a, e1.b, alpha1, e2.a, -alpha2);
    else if( isMatProd(e2) && (i1 || isScaled(e1) || isT(e1)) )
        MatOp_GEMM::makeExpr(res, (e2.flags & ~CV_GEMM_C_T)|(isT(e1) ? CV_GEMM_C_T : 0),
                            e2.a, e2.b, -alpha2, e1.a, alpha1);
    else if( this == e2.op )
        MatOp::subtract(e1, e2, res);
    else
        e2.op->subtract(e1, e2, res);
}

void MatOp_GEMM::multiply(const MatExpr& e, double s, MatExpr& res) const
{
    res = e;
    res.alpha *= s;
    res.beta *= s;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1495

1496 1497 1498
void MatOp_GEMM::transpose(const MatExpr& e, MatExpr& res) const
{
    res = e;
1499 1500 1501
    res.flags = (!(e.flags & CV_GEMM_A_T) ? CV_GEMM_B_T : 0) |
                (!(e.flags & CV_GEMM_B_T) ? CV_GEMM_A_T : 0) |
                (!(e.flags & CV_GEMM_C_T) ? CV_GEMM_C_T : 0);
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
    swap(res.a, res.b);
}

inline void MatOp_GEMM::makeExpr(MatExpr& res, int flags, const Mat& a, const Mat& b,
                                 double alpha, const Mat& c, double beta)
{
    res = MatExpr(&g_MatOp_GEMM, flags, a, b, c, alpha, beta);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////
Andrey Kamaev's avatar
Andrey Kamaev committed
1512 1513

void MatOp_Invert::assign(const MatExpr& e, Mat& m, int _type) const
1514
{
Andrey Kamaev's avatar
Andrey Kamaev committed
1515 1516
    Mat temp, &dst = _type == -1 || _type == e.a.type() ? m : temp;

1517 1518
    cv::invert(e.a, dst, e.flags);
    if( dst.data != m.data )
Andrey Kamaev's avatar
Andrey Kamaev committed
1519
        dst.convertTo(m, _type);
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
}

void MatOp_Invert::matmul(const MatExpr& e1, const MatExpr& e2, MatExpr& res) const
{
    if( isInv(e1) && isIdentity(e2) )
        MatOp_Solve::makeExpr(res, e1.flags, e1.a, e2.a);
    else if( this == e2.op )
        MatOp::matmul(e1, e2, res);
    else
        e2.op->matmul(e1, e2, res);
}

inline void MatOp_Invert::makeExpr(MatExpr& res, int method, const Mat& m)
{
    res = MatExpr(&g_MatOp_Invert, method, m, Mat(), Mat(), 1, 0);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////
Andrey Kamaev's avatar
Andrey Kamaev committed
1538 1539

void MatOp_Solve::assign(const MatExpr& e, Mat& m, int _type) const
1540
{
Andrey Kamaev's avatar
Andrey Kamaev committed
1541 1542
    Mat temp, &dst = _type == -1 || _type == e.a.type() ? m : temp;

1543 1544
    cv::solve(e.a, e.b, dst, e.flags);
    if( dst.data != m.data )
Andrey Kamaev's avatar
Andrey Kamaev committed
1545
        dst.convertTo(m, _type);
1546 1547 1548 1549 1550 1551 1552 1553
}

inline void MatOp_Solve::makeExpr(MatExpr& res, int method, const Mat& a, const Mat& b)
{
    res = MatExpr(&g_MatOp_Solve, method, a, b, Mat(), 1, 1);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////
Andrey Kamaev's avatar
Andrey Kamaev committed
1554 1555

void MatOp_Initializer::assign(const MatExpr& e, Mat& m, int _type) const
1556
{
Andrey Kamaev's avatar
Andrey Kamaev committed
1557 1558
    if( _type == -1 )
        _type = e.a.type();
1559 1560 1561 1562 1563 1564 1565

    if( e.a.dims <= 2 )
        m.create(e.a.size(), _type);
    else
        m.create(e.a.dims, e.a.size, _type);

    if( e.flags == 'I' && e.a.dims <= 2 )
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
        setIdentity(m, Scalar(e.alpha));
    else if( e.flags == '0' )
        m = Scalar();
    else if( e.flags == '1' )
        m = Scalar(e.alpha);
    else
        CV_Error(CV_StsError, "Invalid matrix initializer type");
}

void MatOp_Initializer::multiply(const MatExpr& e, double s, MatExpr& res) const
{
    res = e;
    res.alpha *= s;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1580

1581 1582 1583
inline void MatOp_Initializer::makeExpr(MatExpr& res, int method, Size sz, int type, double alpha)
{
    res = MatExpr(&g_MatOp_Initializer, method, Mat(sz, type, (void*)0), Mat(), Mat(), alpha, 0);
Andrey Kamaev's avatar
Andrey Kamaev committed
1584 1585
}

1586 1587 1588 1589
inline void MatOp_Initializer::makeExpr(MatExpr& res, int method, int ndims, const int* sizes, int type, double alpha)
{
    res = MatExpr(&g_MatOp_Initializer, method, Mat(ndims, sizes, type, (void*)0), Mat(), Mat(), alpha, 0);
}
1590 1591 1592 1593 1594 1595 1596 1597 1598

///////////////////////////////////////////////////////////////////////////////////////////////////////////

MatExpr Mat::t() const
{
    MatExpr e;
    MatOp_T::makeExpr(e, *this);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1599

1600 1601 1602 1603 1604 1605
MatExpr Mat::inv(int method) const
{
    MatExpr e;
    MatOp_Invert::makeExpr(e, method, *this);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1606

1607

1608
MatExpr Mat::mul(InputArray m, double scale) const
1609 1610
{
    MatExpr e;
1611 1612
    if(m.kind() == _InputArray::EXPR)
    {
1613
        const MatExpr& me = *(const MatExpr*)m.getObj();
1614 1615 1616 1617
        me.op->multiply(MatExpr(*this), me, e, scale);
    }
    else
        MatOp_Bin::makeExpr(e, '*', *this, m.getMat(), scale);
1618 1619 1620 1621 1622 1623 1624 1625 1626
    return e;
}

MatExpr Mat::zeros(int rows, int cols, int type)
{
    MatExpr e;
    MatOp_Initializer::makeExpr(e, '0', Size(cols, rows), type);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1627

1628 1629 1630 1631 1632 1633
MatExpr Mat::zeros(Size size, int type)
{
    MatExpr e;
    MatOp_Initializer::makeExpr(e, '0', size, type);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1634

1635 1636 1637 1638 1639 1640 1641
MatExpr Mat::zeros(int ndims, const int* sizes, int type)
{
    MatExpr e;
    MatOp_Initializer::makeExpr(e, '0', ndims, sizes, type);
    return e;
}

1642 1643 1644 1645 1646 1647
MatExpr Mat::ones(int rows, int cols, int type)
{
    MatExpr e;
    MatOp_Initializer::makeExpr(e, '1', Size(cols, rows), type);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1648

1649 1650 1651 1652 1653 1654 1655
MatExpr Mat::ones(Size size, int type)
{
    MatExpr e;
    MatOp_Initializer::makeExpr(e, '1', size, type);
    return e;
}

1656 1657 1658 1659 1660 1661 1662
MatExpr Mat::ones(int ndims, const int* sizes, int type)
{
    MatExpr e;
    MatOp_Initializer::makeExpr(e, '1', ndims, sizes, type);
    return e;
}

1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
MatExpr Mat::eye(int rows, int cols, int type)
{
    MatExpr e;
    MatOp_Initializer::makeExpr(e, 'I', Size(cols, rows), type);
    return e;
}

MatExpr Mat::eye(Size size, int type)
{
    MatExpr e;
    MatOp_Initializer::makeExpr(e, 'I', size, type);
    return e;
}
Andrey Kamaev's avatar
Andrey Kamaev committed
1676

1677 1678 1679
}

/* End of file. */