Matrix decode_matrix()

in src/hit/api/linearalgebra/encryptedmatrix.cpp [258:296]


    Matrix decode_matrix(const vector<vector<Matrix>> &mats, int trim_height, int trim_width) {
        if (mats.empty() || mats[0].empty()) {
            LOG_AND_THROW_STREAM("Internal error: input to decode_col_vector cannot be empty");
        }

        int height = mats[0][0].size1();
        int width = mats[0][0].size2();

        if (trim_height < 0) {
            trim_height = static_cast<int>(mats.size() * height);
        }
        if (trim_width < 0) {
            trim_width = static_cast<int>(mats[0].size() * width);
        }

        vector<double> linear_matrix;
        linear_matrix.reserve(trim_height * trim_width);
        for (int i = 0; i < mats.size(); i++) {
            if (mats[i].size() != mats[0].size()) {
                LOG_AND_THROW_STREAM("Internal error: all rows in decode_matrix must have the same length; "
                                     << "but " << mats[i].size() << "!=" << mats[0].size());
            }
            // for each Matrix row
            for (int j = 0; j < height && i * height + j < trim_height; j++) {
                for (int k = 0; k < mats[0].size(); k++) {
                    if (mats[i][k].size1() != height || mats[i][k].size2() != width) {
                        LOG_AND_THROW_STREAM(
                            "Internal error: all matrices in decode_matrix must have the same dimensions; "
                            << "expected " << height << "x" << width << ", but got " << mats[i][k].size1() << "x"
                            << mats[i][k].size2());
                    }
                    for (int l = 0; l < width && k * width + l < trim_width; l++) {
                        linear_matrix.emplace_back(mats[i][k].data()[j * width + l]);
                    }
                }
            }
        }
        return Matrix(trim_height, trim_width, linear_matrix);
    }