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
>
>
>