Towel: Custom classes for Matrices?

Created on 8 Jul 2020  ·  4Comments  ·  Source: ZacharyPatten/Towel

I know you can use user-defined classes for matrices, but I cannot go through it. Let's say I want to implement my float:

public class MyFloat
{
    private float value;
    public MyFloat(float value)
    {
        this.value = value;
    }

    public static MyFloat operator *(MyFloat a, MyFloat b) => new MyFloat(a.value * b.value);
    public static MyFloat operator +(MyFloat a, MyFloat b) => new MyFloat(a.value + b.value);
    public static MyFloat operator /(MyFloat a, MyFloat b) => new MyFloat(a.value / b.value);
    public static MyFloat operator -(MyFloat a, MyFloat b) => new MyFloat(a.value - b.value);
    public static MyFloat Zero => new MyFloat(0);
    public static implicit operator MyFloat(float a) => new MyFloat(a);
    public static implicit operator float(MyFloat a) => a.value;
    public static implicit operator MyFloat(int a) => new MyFloat(a); // this one is needed for Constant<T>.Zero
}

Then I create two matrices and try to multiply them

var a = new Matrix<MyFloat>(3, 5);
for (int i = 0; i < a.Rows; i++)
    for (int j = 0; j < a.Columns; j++)
        a[i, j] = 3;

var b = new Matrix<MyFloat>(5, 6);
for (int i = 0; i < b.Rows; i++)
    for (int j = 0; j < b.Columns; j++)
        b[i, j] = 5;

var c = a  * b;
Console.WriteLine(c);

And then it hangs. I think I'm missing something, but couldn't find it in docs or samples, but how do I make it work?

Bug

Most helpful comment

Sorry for the late response. There appears to be an issue in my code. It actually isn't in the Matrix code, it is in my ComputePi method which currently runs when I access the Constant<T> class. This will likely be an easy fix, and I'll probably have the fix ready by tomorrow. :)

What I need to do is either fix the infinite loop happening in the ComputePi method or I need to prevent all the constants from being compute in the Constant<T> class. Even though I am accessing Constant<T>.Zero, it also currently computes Constant<T>.Pi since all members of that class are static fields. I originally did this for performance reasons.

Thanks for Submitting the issue. I'll post on this issue again when it is fixed. :)

All 4 comments

Sorry for the late response. There appears to be an issue in my code. It actually isn't in the Matrix code, it is in my ComputePi method which currently runs when I access the Constant<T> class. This will likely be an easy fix, and I'll probably have the fix ready by tomorrow. :)

What I need to do is either fix the infinite loop happening in the ComputePi method or I need to prevent all the constants from being compute in the Constant<T> class. Even though I am accessing Constant<T>.Zero, it also currently computes Constant<T>.Pi since all members of that class are static fields. I originally did this for performance reasons.

Thanks for Submitting the issue. I'll post on this issue again when it is fixed. :)

I have fixed the issue, although I currently require a few more operators than just the ones you had in your sample. All the test methods for the Towel project are inside the Tools/Towel_Testing project. I added a test method for your sample here:
https://github.com/ZacharyPatten/Towel/blob/a20b32e75b8c3fc903124a70d0fc543ecbe830bd/Tools/Towel_Testing/Mathematics/Matrix.cs#L1737

You can see that I added the following operators:

public static bool operator <=(MyFloat a, MyFloat b) => a.value <= b.value;
public static bool operator >=(MyFloat a, MyFloat b) => a.value >= b.value;
public static bool operator ==(MyFloat a, MyFloat b) => a.value == b.value;
public static bool operator !=(MyFloat a, MyFloat b) => a.value != b.value;
public static MyFloat operator -(MyFloat a) => new MyFloat(-a.value);

As for the ComputePi method... I forgot to ++ a variable... so thanks for catching that. There is still room for improvement, but that fixed your issue at least. :) Here was the line i forgot the ++ on: https://github.com/ZacharyPatten/Towel/blob/a20b32e75b8c3fc903124a70d0fc543ecbe830bd/Sources/Towel/Constant.cs#L64

I have not update the nuget package as of this post, but I will do that in the near future.

Is there a hotfix so I could make it work now? Maybe I should add another method or operator?

I believe this issue was fixed in PR #54. If there is still an issue or further issues, please re-open this issue or open a new issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cmckeegan picture cmckeegan  ·  5Comments

GregariousJB picture GregariousJB  ·  8Comments

ArnisL picture ArnisL  ·  5Comments

kaczart picture kaczart  ·  9Comments

miloja picture miloja  ·  29Comments