Matice

#include <iostream>
#include "lexer.h"
 
class Matice
{
public:
    typedef double T;
    int R;
    int S;
    T * data;
    Matice () : R (0), S (0), data (nullptr) { }
    Matice (int R0, int S0) : R (0), S (0), data (nullptr) { alloc (R0, S0);  }
    ~Matice () { if (data != nullptr) delete[] data; }
    void alloc (int R0, int S0);
    T& operator ( ) (int i, int k);
};
 
void Matice::alloc (int R0, int S0)
{
    if (data != nullptr) delete[] data;
    R = R0;
    S = S0;
    data = new T [R*S];
}
 
inline Matice::T & Matice::operator ( ) (int i, int k)
{
    // if (i < 0 || i >= R || k < 0 || k >= S) throw runtime_error ("Bad index");
    return data [i*R+k];
}
 
int main()
{
    Matice m (10, 10);
    try
    {
        m (1,2) = 10;
        cout <<  m (1, 2) << endl;
    }
    catch (runtime_error err)
    {
        cout << "Neco se nepovedlo: " << err.what () << endl;
    }
 
    cout << "O.K.";
    return 0;
}

Matice využívající jednorozměrná pole

#include <iostream>
#include <cassert>
 
using namespace std;
 
class Mat
{
public:
    double* operator [] (int i) // i-ty radek
    {
        assert (i >= 0 && i < m);
        return & p [i * n];
    }
 
    int M() { return m; }
    int N() { return n; }
 
    void print()
    {
        cout << "{" << endl;
        for (int i = 0; i < m; i++)
        {
            cout << "  [";
            for (int j = 0; j < n; j++)
            {
                cout << p[i * n + j];
                if (j < n - 1) cout << ", ";
 
            }
            cout << "  ]" << endl;
        }
        cout << "}" << endl;
    }
 
    Mat (int m0, int n0) // m radek, n sloupcu
    {
        m = m0;
        n = n0;
        p = new double[m * n];
 
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                (*this)[i][j] = i + j;
                // p[i*n+j] = i + j;
    }
 
    ~Mat()
    {
        delete p;
        p = nullptr;
        m = 0;
        n = 0;
    }
 
private:
    int m, n; // m radek, n sloupcu
    double * p;
};
 
void add (Mat & z, Mat & x, Mat & y) 
{
    int m = z.M();
    int n = z.N();
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
            z[i][j] = x[i][j] + y[i][j];
}
 
int main()
{
    int M = 10;
    int N = 20;
 
    Mat a (M, N);
    Mat b (M, N);
    Mat c (M, N);
 
    for (int i = 0; i < M; i++)
        for (int j = 0; j < N; j++)
        {
            a[i][j] = i + j;
            b[i][j] = 2*(i + j);
        }
 
    add (c, a, b); // c = a + b
 
    a.print();
    b.print();
    c.print();
 
    cout << "O.K.\n";
}

Násobení matic

#include <iostream>
#include <cassert>
 
using namespace std;
 
class Mat
{
public:
    double* operator [] (int i) // i-ty radek
    {
        assert (i >= 0 && i < m);
        return & p [i * n];
    }
 
    int M() { return m; }
    int N() { return n; }
 
    void print()
    {
        cout << "{" << endl;
        for (int i = 0; i < m; i++)
        {
            cout << "  [";
            for (int j = 0; j < n; j++)
            {
                cout << p[i * n + j];
                if (j < n - 1) cout << ", ";
 
            }
            cout << "  ]" << endl;
        }
        cout << "}" << endl;
    }
 
    Mat (int m0, int n0) // m radek, n sloupcu
    {
        m = m0;
        n = n0;
        p = new double[m * n];
 
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                (*this)[i][j] = i + j;
                // p[i*n+j] = i + j;
    }
 
    ~Mat()
    {
        delete p;
        p = nullptr;
        m = 0;
        n = 0;
    }
 
private:
    int m, n; // m radek, n sloupcu
    double * p;
};
 
void add(Mat& z, Mat& x, Mat& y) // z := x + y
{
    int m = z.M();
    int n = z.N();
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
            z[i][j] = x[i][j] + y[i][j];
}
 
void mul (Mat& z, Mat& x, Mat& y) // z := x * y
{
    int m = x.M();
    int n = y.N();
    int t = x.N();
    assert(t == y.M());
    assert(m == z.M());
    assert(n == z.N());
 
    for (int i = 0; i < m; i++)
        for (int j = 0; j < n; j++)
        {
            double sum = 0;
            for (int k = 0; k < t; k++)
                sum = sum + x[i][k] * y[k][j];
            z[i][j] = sum;
        }
}
 
int main()
{
    Mat a (2, 2);
    Mat b (2, 1);
    Mat c (2, 1);
 
    a[0][0] = 2;
    a[0][1] = 0;
    a[1][0] = 0;
    a[1][1] = 1;
 
    b[0][0] = 7;
    b[1][0] = 8;
 
    mul(c, a, b);
 
    a.print();
    b.print();
    c.print();
 
    cout << "O.K.\n";
}
 
zpro/matice.txt · Last modified: 2022/02/03 17:41 by 88.103.111.44
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki