processing4 icon indicating copy to clipboard operation
processing4 copied to clipboard

Give PMatrix a print() method

Open PianoMastR64 opened this issue 1 year ago • 6 comments

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 avatar Dec 16 '23 19:12 PianoMastR64

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.

PianoMastR64 avatar Dec 16 '23 19:12 PianoMastR64

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

vaishnavi192 avatar Feb 03 '24 16:02 vaishnavi192

@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 avatar Feb 08 '24 21:02 PianoMastR64

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 avatar Feb 08 '24 21:02 PianoMastR64

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

PianoMastR64 avatar Feb 08 '24 21:02 PianoMastR64

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 avatar Feb 12 '24 23:02 rndmbluescreen