Example #1
0
    //raise a matrix to power of 2
Matrix sqr(const Matrix& A) {
    
    if (A.rows != A.cols)
        throw SizeException("Matrix must be square to be raised to power of 2");
    else
        return A * A;
}
Example #2
0
void Matrix::Reshape(size_t rows, size_t cols) {
    if (!rows || !cols)
        throw SizeException("Invalid shape");
    
    if (this->rows == rows && this->cols == cols) return; //nothing to do here
    
    std::vector<double>::iterator it;
    
    if (rows == 1 && cols == 1) {
        this->M.resize(1);
        goto done;
    } else if (rows == 2 && cols == 2) {
        this->M.resize(4);
        goto done;
    } else if (rows == 3 && cols == 3) {
        this->M.resize(9);
        goto done;
    }
    
        // The simplest case: just resize the array
        // (either losing data either inserting zeros)
    if (this->rows != rows)
        this->M.resize(rows * (this->cols));
    
    this->rows = rows;
    
    it = this->M.begin();
    
        // Now a bit trickier: it's time to deal with
        // data that is located elsewhere in the array
    if (this->cols > cols) {
        for (size_t r = 0; r < this->rows; ++r) {
            this->M.erase(it + cols, it + this->cols);
            it += cols;
        }
    } else if (this->cols < cols) {
        for (size_t r = 0; r < this->rows; ++r) {
            this->M.insert(it + this->cols, cols - this->cols, 0);
            it += cols;
        }
    }
    
    this->cols = cols;
    
done:
    this->rows = rows, this->cols = cols;
    return;
}
Example #3
0
    //return identity matrix
Matrix Matrix::Identity() {
    
    if (this->IsNum()) {
        Matrix k(1,1);
        k.Ones();
        return k;
    }
    
    if (rows != cols)
        throw SizeException("Matrix must be square to have an identity matrix");

    Matrix k(rows,rows);

    for (size_t a = 0; a < rows; a++)
        for (size_t b = 0; b < cols; b++)
            k.M[a * cols + b] = (a==b);
    
    k._isZero = false;
    
    return k;
}
Example #4
0
void Address::encode_address(uint64_t version, uint64_t stream, const SecureVector& ripe)
{
    if(ripe.size() != 20)
        throw SizeException(__FILE__, __FUNCTION__, __LINE__, "The ripe length is not 20");

    SecureVector ripex;

    if(ripe[0] == 0x00 && ripe[1] == 0x00)
        std::copy(ripe.begin() + 2, ripe.end(), std::back_inserter(ripex));
    else if(ripe[0] == 0x00)
        std::copy(ripe.begin() + 1, ripe.end(), std::back_inserter(ripex));
    else ripex = ripe;

    SecureVector bm_addr = encode::varint(version);
    bm_addr += encode::varint(stream);
    bm_addr += ripex;

    SecureVector checksum = hash::sha512(hash::sha512(bm_addr));
    std::copy(checksum.begin(), checksum.begin() + 4, std::back_inserter(bm_addr));

    m_address = encode::base58(bm_addr);
}