DEV Community

Jaipal001
Jaipal001

Posted on

Explaining donut like 5 years old Part-1

I will explain how to write donut in any language

Let R2 = 2, R1 = 1

Image description

To multply it, instead of solving in manually or a matrix solver, we will create a struct (C/C++/Go) or class (Java/Python)

In C

typedef struct {
    double a1;
    double a2;
    double a3;
} singleRow;
Enter fullscreen mode Exit fullscreen mode

Modify according to your language and make sure all members are public

For java

class singleRow {
    public double a1;
    public double a2;
    public double a3;
}
Enter fullscreen mode Exit fullscreen mode

then a matrix struct/class and make all members public

In C

typedef struct {
    singleRow a1;
    singleRow a2;
    singleRow a3;
} Matrix;
Enter fullscreen mode Exit fullscreen mode

For java

class Matrix {
    public singleRow a1;
    public singleRow a2;
    public singleRow a3;
}
Enter fullscreen mode Exit fullscreen mode

To multiply singleRow and a matrix, in C we will create a function and in Java, we will create a public static function in Matrix

singleRow multiply(singleRow m1, Matrix m2) {
    singleRow res;
    res.a1 = (m1.a1 * m2.a1.a1) + (m1.a2 * m2.a2.a1) + (m1.a3 * m2.a3.a1);
    res.a2 = (m1.a1 * m2.a1.a2) + (m1.a2 * m2.a2.a2) + (m1.a3 * m2.a3.a2);
    res.a3 = (m1.a1 * m2.a1.a3) + (m1.a2 * m2.a2.a3) + (m1.a3 * m2.a3.a3);
    return res;
}
Enter fullscreen mode Exit fullscreen mode

Wait for next part

Top comments (0)