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

Give PMatrix a print() method #811

Open
PianoMastR64 opened this issue Dec 16, 2023 · 6 comments · May be fixed by #822
Open

Give PMatrix a print() method #811

PianoMastR64 opened this issue Dec 16, 2023 · 6 comments · May be fixed by #822

Comments

@PianoMastR64
Copy link

Since PMatrix2D and PMatrix3D both have a print() method with matching signatures, it would make sense for their common interface PMatrix to have this method. Right now I have to do this:

public void printMatrix(PMatrix matrix) {
    if(matrix instanceof PMatrix2D matrix2D) {
        matrix2D.print();
    } else if(matrix instanceof PMatrix3D matrix3D) {
        matrix3D.print();
    }
}
@PianoMastR64
Copy link
Author

And since I'm here, usually you would override the toString() method for this so the client can decide what to do with it, such as printing to the console. I'm sure you have your reasons for doing it this way though.

@vaishnavi192
Copy link

vaishnavi192 commented Feb 3, 2024

Hey @benfry @PianoMastR64 do you mean something like this?
public void printMatrix(PMatrix matrix) {
System.out.println(matrix.toString());
}
can you describe a bit further....I want to work on it

@PianoMastR64
Copy link
Author

PianoMastR64 commented Feb 8, 2024

@vaishnavi192 I was thinking something maybe like this:

public interface PMatrix {
    default void print(){
        System.out.println(this);
    }
}

public class PMatrix2D implements PMatrix {
    @Override
    public String toString() {
        int big = (int) abs(max(PApplet.max(abs(m00), abs(m01), abs(m02)),
            PApplet.max(abs(m10), abs(m11), abs(m12))));
        
        int digits = 1;
        if(Float.isNaN(big) || Float.isInfinite(big)) {  // avoid infinite loop
            digits = 5;
        } else {
            while((big /= 10) != 0) digits++;  // cheap log()
        }
        
        StringBuilder sb = new StringBuilder();
        sb
            .append(PApplet.nfs(m00, digits, 4)).append(" ")
            .append(PApplet.nfs(m01, digits, 4)).append(" ")
            .append(PApplet.nfs(m02, digits, 4)).append("\n")
            
            .append(PApplet.nfs(m10, digits, 4)).append(" ")
            .append(PApplet.nfs(m11, digits, 4)).append(" ")
            .append(PApplet.nfs(m12, digits, 4)).append("\n");
        
        return sb.toString();
    }
}

public class PMatrix3D implements PMatrix {
    @Override
    public String toString() {
        int big = (int) Math.abs(max(max(max(max(Math.abs(m00), Math.abs(m01)),
                    max(Math.abs(m02), Math.abs(m03))),
                max(max(Math.abs(m10), Math.abs(m11)),
                    max(Math.abs(m12), Math.abs(m13)))),
            max(max(max(Math.abs(m20), Math.abs(m21)),
                    max(Math.abs(m22), Math.abs(m23))),
                max(max(Math.abs(m30), Math.abs(m31)),
                    max(Math.abs(m32), Math.abs(m33))))));
        
        int digits = 1;
        if(Float.isNaN(big) || Float.isInfinite(big)) {  // avoid infinite loop
            digits = 5;
        } else {
            while((big /= 10) != 0) digits++;  // cheap log()
        }
        
        StringBuilder sb = new StringBuilder();
        sb
            .append(PApplet.nfs(m00, digits, 4)).append(" ")
            .append(PApplet.nfs(m01, digits, 4)).append(" ")
            .append(PApplet.nfs(m02, digits, 4)).append(" ")
            .append(PApplet.nfs(m03, digits, 4)).append("\n")
            
            .append(PApplet.nfs(m10, digits, 4)).append(" ")
            .append(PApplet.nfs(m11, digits, 4)).append(" ")
            .append(PApplet.nfs(m12, digits, 4)).append(" ")
            .append(PApplet.nfs(m13, digits, 4)).append("\n")
            
            .append(PApplet.nfs(m20, digits, 4)).append(" ")
            .append(PApplet.nfs(m21, digits, 4)).append(" ")
            .append(PApplet.nfs(m22, digits, 4)).append(" ")
            .append(PApplet.nfs(m23, digits, 4)).append("\n")
            
            .append(PApplet.nfs(m30, digits, 4)).append(" ")
            .append(PApplet.nfs(m31, digits, 4)).append(" ")
            .append(PApplet.nfs(m32, digits, 4)).append(" ")
            .append(PApplet.nfs(m33, digits, 4)).append("\n");
        
        return sb.toString();
    }
}

@PianoMastR64
Copy link
Author

And since I'm here, I have a suggestion that doesn't have to be taken seriously. I'm just having fun learning about streams and functional programming in Java recently.

This:

int big = (int) Math.abs(max(max(max(max(Math.abs(m00), Math.abs(m01)),
            max(Math.abs(m02), Math.abs(m03))),
        max(max(Math.abs(m10), Math.abs(m11)),
            max(Math.abs(m12), Math.abs(m13)))),
    max(max(max(Math.abs(m20), Math.abs(m21)),
            max(Math.abs(m22), Math.abs(m23))),
        max(max(Math.abs(m30), Math.abs(m31)),
            max(Math.abs(m32), Math.abs(m33))))));

Could be turned into this:

int big = (int) (float) Stream.of(
        m00, m01, m02, m03,
        m10, m11, m12, m13,
        m20, m21, m22, m23,
        m30, m31, m32, m33)
    .map(Math::abs)
    .max(Float::compare)
    .orElse(Float.NaN);

It's a little more readable. Also, I didn't test it.

@PianoMastR64
Copy link
Author

PianoMastR64 commented Feb 8, 2024

Oh, also Float.isNaN() and Float.isInfinite() are being checked on int big which is not a float. Not sure why that is. I'm guessing big was a float in the past

rndmbluescreen added a commit to rndmbluescreen/processing4 that referenced this issue Feb 12, 2024
fixed issue benfry#811 printing a PMatrix now possible
@rndmbluescreen
Copy link

And since I'm here, I have a suggestion that doesn't have to be taken seriously. I'm just having fun learning about streams and functional programming in Java recently.

This:

int big = (int) Math.abs(max(max(max(max(Math.abs(m00), Math.abs(m01)),
            max(Math.abs(m02), Math.abs(m03))),
        max(max(Math.abs(m10), Math.abs(m11)),
            max(Math.abs(m12), Math.abs(m13)))),
    max(max(max(Math.abs(m20), Math.abs(m21)),
            max(Math.abs(m22), Math.abs(m23))),
        max(max(Math.abs(m30), Math.abs(m31)),
            max(Math.abs(m32), Math.abs(m33))))));

Could be turned into this:

int big = (int) (float) Stream.of(
        m00, m01, m02, m03,
        m10, m11, m12, m13,
        m20, m21, m22, m23,
        m30, m31, m32, m33)
    .map(Math::abs)
    .max(Float::compare)
    .orElse(Float.NaN);

It's a little more readable. Also, I didn't test it.

the contribution guidelines say no ;)

also this is my first pull request so let's see how this goes

@rndmbluescreen rndmbluescreen linked a pull request Feb 12, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants