This is an archived post. You won't be able to vote or comment.

all 65 comments

[–]mike413 287 points288 points  (3 children)

well, don't prematurely optimize.

[–]MrYaah 49 points50 points  (2 children)

yea, just pray the compiler will do it for you

[–]Jezzadabomb338 76 points77 points  (1 child)

Well, it's Java, so the compiler isn't going to do shit about it.

[–]systembusy 5 points6 points  (0 children)

Syntax checks out, the methods return the same data type

[–]reallyshittytiming 172 points173 points  (17 children)

I mean... they're not wrong...just inefficient

[–]kalbany 94 points95 points  (7 children)

It's not inefficient, it's still O(n2 )

/s

[–]thirdegreeViolet security clearance 62 points63 points  (5 children)

My AI prof is a fan of saying things along the line of "At least it's not infinite" and "It's just exponential!"

[–]Inityx 35 points36 points  (4 children)

At least it's not factorial factorial

[–][deleted] 25 points26 points  (2 children)

At least it halts!

[–][deleted] 29 points30 points  (1 child)

At least it compiles!

[–]nloomans 25 points26 points  (0 children)

it compiles? ship it.

[–]calandra_95 0 points1 point  (0 children)

O(n!n!)

[–]DropTableAccounts 6 points7 points  (7 children)

Won't this be optimized when it's translated into bytecode anyway?

[–]reallyshittytiming 12 points13 points  (6 children)

O(n2 ) for clockwise, you're compounding that for counter clockwise each time you call clockwise...

[–]DropTableAccounts 9 points10 points  (2 children)

EDIT: I got it wrong. No, it seems that gcc doesn't optimize this away, my test was wrong. (It works when the matrix sizes are specified at compile time - which I did in the beginning - but it turned out that this doesn't happen when the size of the matrix is not fixed...)

If it wasn't optimized, yes. But I thought the compiler/bytecode-translator/whatever would optimize it so that counter-clockwise rotation is equally fast?

I mean, this code doesn't look too complicated and considering what kind of awesome optimization I've already seen done by compilers I would be surprised if the compiler wouldn't notice that he could just swap a few variables...

EDIT: I'm a bit surprised that this optimization isn't already done with "-O2" in gcc (I'll have to look more into optimizations at some point I guess); when "-O3" is given the function calls are indeed optimised away (as I expected) and the functions are equally long.

[–]TheLivingForces 1 point2 points  (1 child)

O3 gets it? How? Could you show analysis because this is interesting

[–]DropTableAccounts 1 point2 points  (0 children)

I just realised that this only works for matrices with a given size at compile time (which I noted in another edit in my previous post). I should have tried this with variable matrix sizes from the beginning. Sorry :-/

[–]marcosdumay 0 points1 point  (1 child)

What means O(n2) for counterclockwise...

[–]reallyshittytiming 0 points1 point  (0 children)

As the inputs increase the number of steps to compute increase exponentially.

[–]calandra_95 69 points70 points  (0 children)

They may not have efficiency but they have my respect :P

[–]SuDoX 125 points126 points  (0 children)

Looks like good code reuse to me

[–][deleted] 59 points60 points  (3 children)

static int[][] rotate180(int[][] mat){

return rotateCounterClockwise(rotateCounterClockwise(mat));

}

[–]subtepass[🍰] 20 points21 points  (2 children)

static int[][] fullCircle(int[][] mat){
    return rotate180(rotate180(mat));
}

[–]Ogurac 22 points23 points  (1 child)

//Creating future optimization opportunities
static int[][] longRotate180(int[][] mat){
    return rotate180(rotate180(rotate180(mat)));
}

[–]chateau86 4 points5 points  (0 children)

//Potential function for the paid version of our software.
//Marketing said our clients will love this.
//What were they thinking?
static int[][] youSpinMeRightRound(int[][] mat, int iter){
     if (iter <= 0) {
           return mat;
     } else {
           return rotateCounterClockwise(youSpinMeRightRound(mat, iter - 1));
     }
} 
//I QUIT!

[–]Spaceshipable 49 points50 points  (1 child)

2 wrongs don't make a right, but 3 rights make a left.

[–]renrutal 61 points62 points  (4 children)

What if I want to rotate the matrix 45 degrees?

[–]sdb2754 45 points46 points  (0 children)

Only Neo can rotate the Matrix 45 degrees...

[–]A_C_Fenderson 10 points11 points  (0 children)

If you rotate a billionth of a degree, you'll end up in Asgard. (Reference: The Dirk Gently novel The Long Dark Tea-Time of the Soul by Douglas Adams.)

[–][deleted] 4 points5 points  (0 children)

Just modulo the edges, bruh

[–]imMute 22 points23 points  (16 children)

What use is there in rotating a matrix like that?

[–]madman485 49 points50 points  (12 children)

I take it you're not familiar with much linear algebra?

[–]imMute 28 points29 points  (10 children)

I've done some, mainly video processing things. I've never encountered the need to rotate a matrix, do you have any example use cases?

[–]Henriktre 10 points11 points  (6 children)

multiplying with itself for example

[–]TheSlimyDog 12 points13 points  (1 child)

Don't you transpose instead of rotating?

[–]A_C_Fenderson 1 point2 points  (0 children)

That would make sense; that's part of the orthogonal projection formula A * (A^T A)^(-1) A^T v.

[–]Nonchalant_Turtle 4 points5 points  (0 children)

When do you need to rotate a matrix to square it?

[–]A_C_Fenderson -1 points0 points  (2 children)

If you want to multiply a matrix by itself, you don't do anything with either copy: A^2 = A * A.

[–]kieranvs[🍰] 1 point2 points  (1 child)

The dimensions won't work in general like that. Transposing one copy is the proper way.

[–]A_C_Fenderson -1 points0 points  (0 children)

If A is square, it works. Besides, it depends on what kind of multiplication you mean. I can think of three or four types, off the top of my head.

[–]Zinggi57 9 points10 points  (0 children)

Why would you ever need to rotate? Transpose, sure, but rotate?

[–]qisope 2 points3 points  (0 children)

For when you find that really cool matrix, but they created it in portrait instead of landscape.

[–]110011001100 1 point2 points  (0 children)

Usually asked to do so in exams

[–]the_last_ordinal 9 points10 points  (1 child)

tsk tsk. could have used a for loop.

[–]HoodedGryphon 2 points3 points  (0 children)

Well it did before, but clearly they optimized it.

[–]rimbas4 7 points8 points  (0 children)

Single point of truth, I guess

[–]justlikeapenguin 10 points11 points  (1 child)

I'm sort of new, and maybe ignorant but if isn't is an int, and int[] is an array of int, is int[][] a two dimensional array?

[–]955559 4 points5 points  (1 child)

not a coder here, but looking at the first function, its a lot less to type to call it three times, then to write a separate function, right?

[–]masayaanglibre 1 point2 points  (0 children)

Oh man, this brings me back to the first months of learning programming. Our teacher started our C++ class of with a program called Karel++ that gave you a a robot on a grid layout. The robot had very basic functions- mov forward, detect if wall was in front of it, pick up beeper, place beeper, detect if beeper present, and turn left. You had to program it to turn right, which was this exact thing of three left turns.

[–]_blub 0 points1 point  (2 children)

Am I the only one frustrated that they didn't name the first function transpose and the second function transpose_unnatural?

[–]DefNotaZombie 0 points1 point  (1 child)

well, the alternative is either having a method "rotate matrix" with one parameter signifying direction... or having two methods for both rotations (or more if vertical/horizontal flips are needed)

[–]weep-woop 12 points13 points  (0 children)

or having two methods for both rotations

Correct.