<div dir="ltr">One last thing I wanted to say!<div><br></div><div>You mentioned that it's nice that Python allows ints and floats to be kept in the same list. I just wanted to issue a warning on that.</div><div><br></div><div>In 99.9% of situations, <i>keep your lists homogeneous</i>. That is, <b><font><font>don't</font></font></b> keep more than one type of data in them. (This goes for any sort of collection).</div><div><br></div><div>The basic operations on lists are essentially: </div><div><br></div><div>1) taking a list and mapping a function over it</div><div>2) taking the list and filtering out items that don't interest you, and </div><div><font><font>3) reducing or folding the list into a summary value</font></font><br></div><div><br></div><div>All three of these require that the items in the list share some common operations. For mapping, you need to supply a function that behaves for every element in the list. For filtering, you need to supply a test that makes sense for all elements. And for reduction, you need some binary operation that works with each element.</div><div><br></div><div>So while it may seem neat that you can store elements of different types, if you end up with, say, a list containing bools and ints together, the number of "shared operations" that work on these is much smaller than either a homogeneous list of bools or a homogeneous list of ints separately.</div><div><br></div><div>It <i>is</i> possible in Python to do a run-time test of what type an object is, (and then presumably doing something intelligent based on the result). However, it is arguably bad coding practice. It's easier to reason about code that acts "uniformly" on their inputs, rather than dispatching based on type. Runtime type inspection is also slightly nuanced when dealing with subtyping, and in Python, it is limited in what it can tell you. (You can tell an integer is an integer, but given a function, you can't tell what inputs are valid nor what outputs to expect back from it).</div><div><br></div><div>In practice, mathematical and algorithmic code tends to be specialized to one data type. (And perhaps re-implemented separately for different types, say for single-precision floats then for double-precision floats). This is because performance is often paramount in these domains. But just realize that this kind of thing is done because it's necessary, not because it's a good thing to do. </div><div><br></div><div>If execution speed wasn't an issue, you would want to parametrize your matrix code by the data type. Java can do this (to some degree) with subtyping and generic types. (It's a non-issue for Python because of its lack of static typing). </div><div><br></div><div>At the risk of alienating some people, in mathematics, matrices are commonly done over some arbitrary ring. (A ring is a set of numbers which support addition, subtraction, and multiplication, but may or may not support division). In Java, you might consider defining an abstract base class called Ring with four methods: <font face="courier new, monospace">add</font>, <font face="courier new, monospace">negate</font>, and <font face="courier new, monospace">multiply</font>. Then, for your matrix code, instead of working with <font face="courier new, monospace">int[][]</font>'s or <font face="courier new, monospace">double[][]</font>'s, you would have <font face="courier new, monospace">Ring[][]</font><font face="arial, helvetica, sans-serif">. Then, during matrix multiplication, any place you would use </font><font face="courier new, monospace">matrix1[i][j] * matrix2[j][k]</font><font face="arial, helvetica, sans-serif">, you would instead write it as </font><font face="courier new, monospace">matrix1[i][j].multiply(matrix2[j][k])</font><font face="arial, helvetica, sans-serif"> (and analogously for addition).</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">Then, you could create an </font><font face="courier new, monospace">IntRing</font><font face="arial, helvetica, sans-serif"> class where multiplication is just integer multiplication, etc, and a </font><font face="courier new, monospace">FloatRing</font><font face="arial, helvetica, sans-serif"> with the operations for floats. I could also later decide to create a </font><font face="courier new, monospace">ComplexRing</font><font face="arial, helvetica, sans-serif">, and now, without any changes to my code (which I made into a library and published to Github), now works for complex numbers. I could also create a </font><font face="courier new, monospace">ModularIntRing</font><font face="arial, helvetica, sans-serif">, where operations are taken mod some number for my (slow-ass, unverified) crypto implementations! I could even have really fancy classes like </font><font face="courier new, monospace">PolynomialRing</font><font face="arial, helvetica, sans-serif"> or </font><font face="courier new, monospace">PowerSeriesRing</font><font face="arial, helvetica, sans-serif">, and now I can do a bit of symbolic mathematics. Or I might make my </font><font face="courier new, monospace">Matrix</font><font face="arial, helvetica, sans-serif"> class itself a </font><font face="comic sans ms, sans-serif">Ring</font><font face="arial, helvetica, sans-serif">, and now I can work with block matrices (with square blocks).</font></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">Of course, again, none of this matters in Python. Python trades away any sort of static guarantees about your program for an incredible amount of flexibility. The Java version, on the other hand, makes you jump through more hoops up front, but it will also catch more errors at compile time. </font></div><div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">I think I got carried away. If any of this doesn't make sense, just ignore it!</font></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Oct 10, 2014 at 3:11 PM, Michael Maloney <span dir="ltr"><<a href="mailto:tac@tac-tics.net" target="_blank">tac@tac-tics.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">@Philip, I believe Douglas is taking a class, so unfortunately, Clojure is probably not an option. (Although I encourage anyone to look at Clojure).<div><br></div><div>A few comments on the code:</div><div><br></div><div>Watch your alignment. On line 11, for example, the block inside the main method lines up with the declaration. You want to tab it. Even though Java doesn't enforce indentation, you should pretend it is. On line 18 and other places, you've tabbed the curly brace. This is a relatively stylistic choice. (I think C programmers use it still, though?) Java's official style guide says opening curly braces should come at the end of the same line, closing curly braces should line up with the if/for/while statement or method declaration that opened it:</div><div><br></div><div><font face="courier new, monospace">while (...) {</font></div><div><font face="courier new, monospace"><font><font>  Â  // ...</font></font></font></div><div><font face="courier new, monospace"><font><font>  Â  // ...</font></font><br></font></div><div><font face="courier new, monospace"><font><font><font><font>  Â  // ...</font></font></font></font></font><br></div><div><font face="courier new, monospace"><font><font>}</font></font></font></div><div><br></div><div>At 101 lines of code, your main method is excruciatingly long. Most methods you write should to be between 1 and ~8 lines long. Highly algorithmic code (say, an implementation of a mergesort) might be around 30 lines long. The main method of a script might be that long too in some cases. But 101 is enough to exhaust anyone's attention. As I mention in the other email I sent, you shouldn't need to double-space all of your code. And you should consider splitting the main method up into separate smaller "helper" methods. </div><div><br></div><div>A good way to do this might be to break out these pieces: the code to read the user's dimension input (~18 lines), the user's choice of data type (~10 lines), the user's entry input (~20 lines), and the output code (~30 lines).</div><div><br></div><div>Even in cases where splitting a method up into separate pieces doesn't decrease the total line count of your program, it often helps your codes readability considerably. Especially if you choose your method names carefully, it's easier to glance at the function call and <i>guess</i> what it should be doing, without having to be presented with the gory details. </div><div><br></div><div>I know none of this addresses your question directly, but often, having your code more organized will help you isolate the errors you run into. Rearranging your code to make it more understandable is what we call this refactoring. I don't know about the rest of the community, but I've always found it very relaxing. Like trimming a bonzai tree or raking the sand in a zen garden :)</div><div><br></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Oct 10, 2014 at 1:21 PM, Philip Doctor <span dir="ltr"><<a href="mailto:diomedestydeus@gmail.com" target="_blank">diomedestydeus@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><br></div><div>For console output I'd recommend checking the docs on how to pad numbers into columns (<a href="http://docs.oracle.com/javase/tutorial/java/data/numberformat.html" target="_blank">http://docs.oracle.com/javase/tutorial/java/data/numberformat.html</a>).  If this is not a homework assignment and you're allowed to use 3rd party libraries I've never used it but I heard good things about <a href="https://code.google.com/p/j-text-utils/" target="_blank">https://code.google.com/p/j-text-utils/</a> .</div><div><br></div><div>Of course if you're not doing it for a class I'm not totally sure why you would reinvent the wheel on matrix multiplication as there's tons good math libraries out there for java that will do this:</div><div><br></div><div><a href="http://commons.apache.org/proper/commons-math/" target="_blank">http://commons.apache.org/proper/commons-math/</a><br></div><div><a href="http://math.nist.gov/javanumerics/jama/" target="_blank">http://math.nist.gov/javanumerics/jama/</a><br></div><div><a href="https://code.google.com/p/efficient-java-matrix-library/" target="_blank">https://code.google.com/p/efficient-java-matrix-library/</a><br></div><div><br></div><div>(a dozen more if you google it)</div><div><br></div><div>Best of luck (p.s. if jvm is a requirement but java isn't, I'm going to fan-boy plug clojure as a language you might enjoy more given your statements about python).</div><div><br></div><div>/off-topic</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div>On Fri, Oct 10, 2014 at 12:43 PM, Lewit, Douglas <span dir="ltr"><<a href="mailto:d-lewit@neiu.edu" target="_blank">d-lewit@neiu.edu</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><div dir="ltr"><font size="4">This is probably the wrong forum for this, but I thought I would give it a try because the people at my university cannot always be counted on for good feedback. Â </font><div><font size="4"><br></font></div><div><font size="4">I wrote this Java program that multiples two matrices.  I think it's basically pretty good. Â (And doing this in Python is WAY EASIER because Python doesn't distinguish between lists of ints and lists of doubles, and actually allows both data types to get combined in the same list. )</font></div><div><font size="4"><br></font></div><div><font size="4">However, I'm having some issues with the formatted output of my "float" matrices.  They are technically doubles, but in the program I refer to them as floating point values for the sake of clarity because some users of the program may not know what a double is.  Is that like a double martini? Â : ) Â Â </font></div><div><font size="4"><br></font></div><div><font size="4">I'm trying to get all of my numbers lined up properly in their respective columns, but it's just not working out that way, even with the <b>printf </b>command.....??? Â </font></div><div><font size="4"><br></font></div><div><font size="4">If anyone can offer some good suggestions about good formatting, that would be great.  I would really appreciate it.</font></div><div><font size="4"><br></font></div><div><font size="4">By the way, I did the same thing in Python and it took less than half as much code!  The Python code was short and to the point.  I guess Java has its uses, but for some things it is really tedious and overly complicated.  Ah well.... but then again Java developers make really good money, so I guess I'll have to study both Java AND Python!</font></div><div><font size="4"><br></font></div><div><font size="4">Take care and thanks for the feedback.</font></div><div><font size="4"><br></font></div><div><font size="4">Best,</font></div><div><font size="4"><br></font></div><div><font size="4">Douglas Lewit</font></div></div>
<br></div></div>_______________________________________________<br>
Chicago mailing list<br>
<a href="mailto:Chicago@python.org" target="_blank">Chicago@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/chicago" target="_blank">https://mail.python.org/mailman/listinfo/chicago</a><br>
<br></blockquote></div><br></div>
<br>_______________________________________________<br>
Chicago mailing list<br>
<a href="mailto:Chicago@python.org" target="_blank">Chicago@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/chicago" target="_blank">https://mail.python.org/mailman/listinfo/chicago</a><br>
<br></blockquote></div><br></div>
</div></div></blockquote></div><br></div>