Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom classes for Matrices? #53

Closed
WhiteBlackGoose opened this issue Jul 8, 2020 · 4 comments
Closed

Custom classes for Matrices? #53

WhiteBlackGoose opened this issue Jul 8, 2020 · 4 comments
Assignees
Labels
Bug issues that suggest a flaw with existing code
Milestone

Comments

@WhiteBlackGoose
Copy link
Contributor

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?

@ZacharyPatten
Copy link
Owner

ZacharyPatten commented Jul 9, 2020

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. :)

ZacharyPatten added a commit that referenced this issue Jul 11, 2020
ZacharyPatten added a commit that referenced this issue Jul 11, 2020
@ZacharyPatten
Copy link
Owner

ZacharyPatten commented Jul 11, 2020

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:

[TestMethod] public void GithubIssue53()

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:

predicate = PI => ++iterations < 100;

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

@ZacharyPatten ZacharyPatten self-assigned this Jul 11, 2020
@ZacharyPatten ZacharyPatten added the Bug issues that suggest a flaw with existing code label Jul 11, 2020
@ZacharyPatten ZacharyPatten added this to the First Release milestone Jul 11, 2020
ZacharyPatten added a commit that referenced this issue Jul 11, 2020
@WhiteBlackGoose
Copy link
Contributor Author

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

@ZacharyPatten
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug issues that suggest a flaw with existing code
Projects
None yet
Development

No branches or pull requests

2 participants