Results 1 to 9 of 9
  1. #1
    Armadillo is offline Member
    Join Date
    Jul 2010
    Posts
    15
    Rep Power
    0

    Default Java arithemetic

    Hi guys,

    Does anyone know a simple Java arithemetic library? I am writing a program where i do really basic stuff like multiplying every value in a list of Integers with some factor, then the same for a list of BigDecimals. Summing over listst etc.

    I find myself writing generic methods and methods for summing over objects of type Number. This must have been done a thousand times by a thousand other people.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Armadillo View Post
    Hi guys,

    Does anyone know a simple Java arithemetic library? I am writing a program where i do really basic stuff like multiplying every value in a list of Integers with some factor, then the same for a list of BigDecimals. Summing over listst etc.

    I find myself writing generic methods and methods for summing over objects of type Number. This must have been done a thousand times by a thousand other people.
    Java is not known for its fast arithmetic; maybe the following link can be of help. Most of the stuff is written in Fortran (or C or C++) but you can find some Java stuff in there as well. For your 'level 1' stuff (BLAS terminology) you can easily write it yourself though; it can be fun writing it ...

    kind regards,

    Jos

  3. #3
    Armadillo is offline Member
    Join Date
    Jul 2010
    Posts
    15
    Rep Power
    0

    Default

    Thanks for your fast reply Jos,
    I'm not so much looking for a fast library - i'm summing production per year so my lists have length 12. Then i also multiply the discrete numbers with decimals so that i have a length 12 list of BigDecimals. Easy of use comes before speed in this case :-)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Armadillo View Post
    Thanks for your fast reply Jos,
    I'm not so much looking for a fast library - i'm summing production per year so my lists have length 12. Then i also multiply the discrete numbers with decimals so that i have a length 12 list of BigDecimals. Easy of use comes before speed in this case :-)
    Well, what are you waiting for then? That functionality is extremely easy to implement ...

    kind regards,

    Jos

  5. #5
    Armadillo is offline Member
    Join Date
    Jul 2010
    Posts
    15
    Rep Power
    0

    Default

    That all very well (ok, its a hassle). But now i have a method that sums all the values in a list of objects that extend Number. It should return a Number of the input type, so summing a list of Integers yields an Integer, summing a list of Long returns a Long etc.

    I can implement this method for non empty lists, because i can get the objects inthe list and get their class. If the list itself is null, i just return null.

    So now what is the list is empty? My method should return 0. Specifically, it should return a 0 of the same type as the objects (of which there are none) in the empty input list.

    Do you see my problem? I cannot know the exact type of the 0 as i dont have an object whose class i can query.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Can you make a small program that compiles to demonstrate your problem?

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Armadillo View Post
    Do you see my problem? I cannot know the exact type of the 0 as i dont have an object whose class i can query.
    I understand your problem; unfortunately there is no way during runtime to find the type Double in, say, List<Double> due to type erasure; if the list is empty (i.e. not containing at least one Double) you're out of luck. Would it be possible to keep an 'exemplar'? (a SmallTalk thingie). The type of the exemplar would be the type of the elements in your collection. Even a String representation of the type will do.

    kind regards,

    Jos

  8. #8
    Armadillo is offline Member
    Join Date
    Jul 2010
    Posts
    15
    Rep Power
    0

    Default

    I decided to cheat a little and return a BigDecimal whatever kind of (number) list i start with, see below.

    @Jos
    This Smalltalk, i am puzzled how it never got to be 'big' when i hear so many good things about it from so many people. (programmers anyway)

    @Norm
    This is what i'm doing now. Ideally the addOverAnySubList method would return a value of type T

    Java Code:
    /**
    	 * Adds values over sublist of this. 
    	 * The sublist is exclusive: 
    	 * addOverSubList([1, 2, 3], 0, 2) = 3
    	 * returns 0 if the list was null or empty
    	 */
    	public static <T extends Number> BigDecimal addOverAnySubList(List<T> values, int from, int to){
    		if (values == null) {
    			return new BigDecimal(0);
    		} else if (to > (values.size())) {
    			return addOverAnySubList(values, from, (values.size()));
    		} else if((from > values.size() - 1 || from > to)) {
    			throw new IllegalArgumentException("The start position is larger then the list size or is after the end position.");
    		} else {
    			List<T> sub = values.subList(from, to);
    			BigDecimal tot = new BigDecimal(0);
    			for (T b:sub) {
    				tot.add(getBigDecimal(b));
    			}
    			return tot;
    		}
    	}
    	
    	private static <T extends Number> BigDecimal getBigDecimal(T num) {
    		if (num == null) {
    			return new BigDecimal(0);
    		} else if (num instanceof Double)  {
    			double num12 = (Double) num;
    			return new BigDecimal(num12);
    		} else if (num instanceof Integer) {
    			int num12 = (Integer) num;
    			return new BigDecimal(num12);
    		} else if (num instanceof BigDecimal) {
    			return (BigDecimal) num;
    		} else if (num instanceof Float)  {
    			double num12 = ((Float) num).doubleValue();
    			return new BigDecimal(num12);
    		} else if (num instanceof Long) {
    			long num12 = (Long) num;
    			return new BigDecimal(num12);
    		} else {
    			throw new UnsupportedOperationException("Not yet implemented for AtomicInteger, AtomicLong, BigInteger, Byte and Short");
    		}
    	}

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Armadillo View Post
    @Jos
    This Smalltalk, i am puzzled how it never got to be 'big' when i hear so many good things about it from so many people. (programmers anyway)
    SmallTalk was an anachronism; the main stream computing community wasn't up to it yet (it was all Pacal, C, Fortran etc in those days). They praise Java now and almost turn it into a religion, but SmallTalk already had it all in those days (and even better ;-)

    kind regards,

    Jos

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •