Re: [julia-users] How to create empty type and then append stuff to it?

926 views
Skip to first unread message

John Myles White

unread,
Jun 4, 2013, 10:05:20 AM6/4/13
to julia...@googlegroups.com
Try either of the following to create an empty array of mvmonial's:

mvmonomial[]

Array(mvmonomial, 0)

As a really minor style point, it's helpful to make the names of types start with capital letters.

-- John

On Jun 4, 2013, at 8:57 AM, Stuart Brorson <s...@cloud9.net> wrote:

> Hi everybody,
>
> I'm finally in a position to spend quality time fiddling with Juila. As a start, I am trying to create a set of types implementing multivariate polynomials. (For example, 3*x1^2*x2 + 5*x1*x2^2).
>
> The basic type is a multivariate monomial, which represents each term in the polynomial. The monomial is a type (struct) containing the coeff and a vector of powers. I use BigInts as the coeff type since I later want to do things which can produce extra-large, integer polynomial coefficients.
>
> -----
> type mvmonomial
> c::BigInt
> p::Vector{Int64}
> end
>
> # Declare a constructor taking Int64 coefficients
> mvmonomial(c::Int64, p::Vector{Int64}) = mvmonomial(BigInt(c), p)
> -----
>
> In the above, I declare a constructor overload which allows me to create monomials using normal ints (rather than have to cast my inputs all the time). This is simply a convenience function.
>
> Next, I define a type for the polynomial itself. This should be simply a vector of terms (i.e. a vector of individual monomials):
>
> -----
> type mvpoly
> terms::Vector{mvmonomial}
> end
> -----
>
> Now comes the problem. I am writing a function in which want to create an empty mvpoly and then append stuff to it in a loop. My ideal code would look like this:
>
> -----
> function collect_terms(Q::mvpoly)
> R = mvpoly() # create empty mvpoly
> for t in Q.terms
> if contains(R.terms, t)
> # add this monomial to the existing monomial named u
> idx = findin(R.terms, t)
> u = R.terms[idx]
> u.c += t.c
> R.terms[idx] = u
> else
> # Append this monomial to R
> R = union(R, mvpoly(t))
> end
> end
> return R
> end
> -----
>
> To make this work, I have tried several ways to construct an empty mvpoly using the empty list [], for example
>
> -----
> mvpoly() = mvpoly(mvmonomial(0, []))
> -----
>
> However, no matter what I do, I can't get the type system to do what I want, i.e. create an empty mvpoly type which I can append to.
>
> Is there a clean, Julian way to implement this functionality?
>
> Thanks for any insights you may have,
>
> Stuart
>
>
>

Stuart Brorson

unread,
Jun 4, 2013, 6:29:01 PM6/4/13
to julia...@googlegroups.com
Thanks, John!

Hmmm.... I have fiddled around with your suggestion, and it indeed
works. However, I am still misunderstanding something since I am not
getting what I want.

When I create the empty array using

mvmonomial[]

what am I creating? It seems to be an array whose elements are
mvmonomials (or will be when I fill it in). The object itself is
simply Array.

What I'd like to do is create a object called "mvpoly" which is an
array whose elements are mvmonomials. I want this because I can then
create overloads for polynomial addition, multiplication, etc. which
are called for mvpoly input types.

I tried doing this using a typealias, but no joy.

So I guess my question is: How does one create a new type which acts
like an array of underlying composite types? Is there a julian
idiom for this?

As for declaring types using caps, yup, good point. I'll get better
at this as I work on it. Thanks.

Stuart

Stefan Karpinski

unread,
Jun 4, 2013, 9:45:18 PM6/4/13
to Julia Users
On Tue, Jun 4, 2013 at 6:29 PM, Stuart Brorson <s...@cloud9.net> wrote:
Thanks, John!

Hmmm....  I have fiddled around with your suggestion, and it indeed
works.  However, I am still misunderstanding something since I am not
getting what I want.

When I create the empty array using

mvmonomial[]

what am I creating?  It seems to be an array whose elements are
mvmonomials (or will be when I fill it in).  The object itself is
simply Array.

The type of the object is Array{MvMonomial}, not just Array. Array is an abstract type that includes all specific instances of Array{T}, including Array{Any}, which is not the same thing as Array – Array{Any} is a *concrete* type, namely the type of an array that can hold any kind of value.
 
What I'd like to do is create a object called "mvpoly" which is an
array whose elements are mvmonomials.  I want this because I can then
create overloads for polynomial addition, multiplication, etc. which
are called for mvpoly input types.

You mean you want a new type called MvPoly? In that case you need to define a new type that wraps an array in order to give it new behavior. You probably want to use an immutable type, assuming that you're not going to want to change which array object it wraps:

immutable MvPoly
  data::Array{MvMonomial}
end

I tried doing this using a typealias, but no joy.

Yes, typealias does not create a distinct new type but rather an alias for an existing type. Thus, which you write `typealias MvPoly Array{MvMonomial}` you're just defining a shorthand for the Array{MvMonomial} type, not creating a new type.
 
So I guess my question is:  How does one create a new type which acts
like an array of underlying composite types?  Is there a julian idiom for this?

I hope the above clarifies.
 
As for declaring types using caps, yup, good point.  I'll get better
at this as I work on it.  Thanks.

:-) 

Viral Shah

unread,
Jun 4, 2013, 11:16:57 PM6/4/13
to julia...@googlegroups.com
Hi Stuart,

Great to see you back here!

-viral

Stuart Brorson

unread,
Jun 5, 2013, 7:53:35 AM6/5/13
to julia...@googlegroups.com
Thanks, Viral! I'm having fun playing around.

But let's see if you say that once I file a few bug reports....

:-)

Stuart
Reply all
Reply to author
Forward
0 new messages