Mathematical set operations in Java / searching for library

Discussion in 'Java' started by mowsen@googlemail.com, Nov 3, 2007.

  1. Guest

    Dear Group,


    i'm searching for a Java library which supports all ways of set
    handling, like generating a set of all prime numbers up to 10000, diff
    it to another set and so on.

    Anyone a hint?

    Thanks,
    mowsen
     
    , Nov 3, 2007
    #1
    1. Advertising

  2. Roedy Green Guest

    Roedy Green, Nov 4, 2007
    #2
    1. Advertising

  3. On 03.11.2007 23:58, wrote:
    > i'm searching for a Java library which supports all ways of set
    > handling, like generating a set of all prime numbers up to 10000, diff
    > it to another set and so on.


    java.util.Set and implementors. Google will yield plenty of prime
    number generation algorithms.

    robert
     
    Robert Klemme, Nov 4, 2007
    #3
  4. Guest

    thanks. i'm not quite sure, but is it possible to define for example
    all natural numbers easily with enumset/set?!

    thanks,
    moka
     
    , Nov 7, 2007
    #4
  5. Eric Sosman Guest

    wrote:
    > thanks. i'm not quite sure, but is it possible to define for example
    > all natural numbers easily with enumset/set?!


    If you can afford that much memory, I want to be your friend.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Nov 7, 2007
    #5
  6. In article <>,
    <> wrote:
    >thanks. i'm not quite sure, but is it possible to define for example
    >all natural numbers easily with enumset/set?!


    No. One reason is that the interface java.util.Set<E> requires you to
    implement:

    int size() - Returns the number of elements in this set (its
    cardinality).

    And the int data type does not define a bit pattern that means
    "infinite". You could therefore not implement an infinite size set
    (such as N) using java.util.Set while honouring the interface
    requirements for that type.

    Cheers,
    Bent D
    --
    Bent Dalager - - http://www.pvv.org/~bcd
    powered by emacs
     
    Bent C Dalager, Nov 7, 2007
    #6
  7. On 07.11.2007 11:29, wrote:
    > thanks. i'm not quite sure, but is it possible to define for example
    > all natural numbers easily with enumset/set?!


    You did not mention infinite sets in your original posting...

    I don't know what you intend to do but maybe a math system like
    mathematica and similar is better suited to your needs. There are even
    free implementations available.

    Cheers

    robert
     
    Robert Klemme, Nov 10, 2007
    #7
  8. Stefan Ram Guest

    writes:
    >thanks. i'm not quite sure, but is it possible to define for
    >example all natural numbers easily with enumset/set?!


    The set of all Java Integer objects (just written - never
    compiled, so there will still be errors, but you get the idea):

    class Integers extends java.util.AbstractSet implements java.util.Set
    { public Integers(){} @java.lang.Override
    public void clear(){ throw new java.lang.UnsupportedOperationException; }
    @java.lang.Override public boolean contains( final java.lang.Object key )
    { return java.lang.Integer.class.isAssignableFrom( key.getClass()); }
    @java.lang.Override public boolean remove( final java.lang.Object key )
    { throw new java.lang.UnsupportedOperationException; }
    @java.lang.Override public int size()
    { return java.lang.Integer.MAX_VALUE; }
    @java.lang.Override public boolean add( final java.lang.Object arg0 )
    { throw new java.lang.UnsupportedOperationException; }
    @java.lang.Override public java.util.Iterator iterator()
    { return new java.util.Iterator()
    { int i = java.lang.Integer.MIN_VALUE;
    public boolean hasNext(){ return i + 1 > i; }
    public java.lang.Object next(){ return ++i; } public void remove()
    { throw new java.lang.UnsupportedOperationException; }}}}

    Mathematical integers can not all be represented as distinct
    objects or values by a computer, because they are not limited
    in size, so a storage for a single mathematical integer value
    would need to have more states the possible different states
    of our whole universe. This also holds for storages of sets of
    mathematical integers.
     
    Stefan Ram, Nov 10, 2007
    #8
  9. Daniel Pitts Guest

    Stefan Ram wrote:
    > writes:
    >> thanks. i'm not quite sure, but is it possible to define for
    >> example all natural numbers easily with enumset/set?!

    >
    > The set of all Java Integer objects (just written - never
    > compiled, so there will still be errors, but you get the idea):
    >
    > class Integers extends java.util.AbstractSet implements java.util.Set
    > { public Integers(){} @java.lang.Override
    > public void clear(){ throw new java.lang.UnsupportedOperationException; }
    > @java.lang.Override public boolean contains( final java.lang.Object key )
    > { return java.lang.Integer.class.isAssignableFrom( key.getClass()); }
    > @java.lang.Override public boolean remove( final java.lang.Object key )
    > { throw new java.lang.UnsupportedOperationException; }
    > @java.lang.Override public int size()
    > { return java.lang.Integer.MAX_VALUE; }
    > @java.lang.Override public boolean add( final java.lang.Object arg0 )
    > { throw new java.lang.UnsupportedOperationException; }
    > @java.lang.Override public java.util.Iterator iterator()
    > { return new java.util.Iterator()
    > { int i = java.lang.Integer.MIN_VALUE;
    > public boolean hasNext(){ return i + 1 > i; }
    > public java.lang.Object next(){ return ++i; } public void remove()
    > { throw new java.lang.UnsupportedOperationException; }}}}
    >
    > Mathematical integers can not all be represented as distinct
    > objects or values by a computer, because they are not limited
    > in size, so a storage for a single mathematical integer value
    > would need to have more states the possible different states
    > of our whole universe. This also holds for storages of sets of
    > mathematical integers.
    >


    While the statement is true, the representation of infinite sets can be
    accomplished in finite space. You only need to abstract the behavior of
    such a set.

    For instance, setOfPrimes.intersect(setOfEvenNumbers) would be equal to
    the set that contains "2".

    While, I wouldn't want to write the implementation of those objects, it
    is a feasible undertaking if you add appropriate restrictions on the
    number of different types of sets you could do.

    Also, if you can create a formula to iterate or calculate certain
    properties of the set, then you needn't represent all the values in the set.

    in setOfEvenNumbers:
    public boolean contains(Integer o) {
    return o.intValue() % 2 == 0;
    }

    You might build an abstract interface to mathematical sets that is
    different than the Java "Set" interface, as generally in Mathematics,
    you specify rules, conditions, relationships, etc. for inclusion in a
    set, rather than adding individual elements. Java Collections are very
    specifically designed to "hold" collections of Java Objects, not
    mathematical abstractions.

    There may or may not be such a library already created. I would google
    for it before discounting the possibility.

    Good luck,
    Daniel.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Nov 10, 2007
    #9
  10. Stefan Ram Guest

    Daniel Pitts <> writes:
    >you specify rules, conditions, relationships, etc. for inclusion in a


    This is also known as »symbolic mathematics«
    in contrast to »numerical mathematics«.

    >There may or may not be such a library already created.
    >I would google for it before discounting the possibility.


    Or even use an old-fashioned catalogue, such as:

    http://www.google.com/Top/Science/Math/Software/

    (Including some Java software or libraries.)
     
    Stefan Ram, Nov 10, 2007
    #10
  11. Stefan Ram Guest

    writes:
    >i'm searching for a Java library which supports all ways of set
    >handling, like generating a set of all prime numbers up to 10000, diff
    >it to another set and so on.


    The efficient implementation depends on knowledge
    of implementation and expected usage (calls).

    A SetDiff, for example, could be done as

    .... class DiffSet ...
    { public Diff( final java.util.Set a, final java.util.Set b ) ...

    public boolean contains( final java.lang.Object o )
    { return a.contains( o )&& !b.contains( o ); }

    public java.util.Iterator iterator()
    { ... /* Use the iterator of a, skipping when b.contains( ... ) */ ... }

    A well-known DSL for set operations is SQL.
     
    Stefan Ram, Nov 10, 2007
    #11
  12. Lew Guest

    Stefan Ram wrote:
    > A SetDiff, for example, could be done as
    >
    > .... class DiffSet ...
    > { public Diff( final java.util.Set a, final java.util.Set b ) ...
    >
    > public boolean contains( final java.lang.Object o )
    > { return a.contains( o )&& !b.contains( o ); }
    >
    > public java.util.Iterator iterator()
    > { ... /* Use the iterator of a, skipping when b.contains( ... ) */ ... }
    >
    > A well-known DSL for set operations is SQL.


    Another way to handle diffs:

    public static <T> Set <T> diff( Set<? extends T> a, Set<? extends T> b )
    {
    Set <T> diff = new HashSet <T> ( a );
    diff.removeAll( b );
    return diff;
    }

    If you don't mind changing the contents of 'a', youi can skip the wrapper
    method and just use a.removeAll( b ).

    --
    Lew
     
    Lew, Nov 10, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MtnSurf8

    Mathematical Operations in VHDL

    MtnSurf8, Apr 25, 2004, in forum: VHDL
    Replies:
    1
    Views:
    5,948
    Egbert Molenkamp
    Apr 25, 2004
  2. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    4,323
    robert
    Feb 11, 2006
  3. mc
    Replies:
    2
    Views:
    289
    Jaap Spies
    Apr 7, 2008
  4. Romeo Kienzler
    Replies:
    0
    Views:
    534
    Romeo Kienzler
    Jul 30, 2009
  5. Replies:
    3
    Views:
    808
    Terry Reedy
    Apr 1, 2011
Loading...

Share This Page