How do you use as an array as an argument

H

Hakusa

I have this class Term (of the algebraic kind) and I want it to accept
an array of variables and an array of coefficients (in case I get
something like 2x*2y). I have no problems with the constructor or
anything else, but I can't figure out how to make the constructor
accept an array as an argument without resorting to arbitrary array
creation and then deleting the array later. It feels unnecessary.
 
M

Mark Space

I have this class Term (of the algebraic kind) and I want it to accept
an array of variables and an array of coefficients (in case I get
something like 2x*2y). I have no problems with the constructor or
anything else, but I can't figure out how to make the constructor
accept an array as an argument without resorting to arbitrary array
creation and then deleting the array later. It feels unnecessary.

I'm honestly not sure what you are asking. Some examples of how it
works/you want it to work would be helpful.

Do you want overloaded constructors?

Term( Double t ) {} // One coefficient
Term( Double [] ts ) {} // Multiple coefficients

This gives you a Term that could be constructed with either one number,
or an array of numbers:

Term t1 = new Term( 10.1 );
Term t2 = new Term( Double [] = {10.1, 12.2} };

Don't quote me on the syntax, I didn't check it and I don't often use
anonymous arrays ;-)
 
M

Mark Rafn

I have this class Term (of the algebraic kind) and I want it to accept
an array of variables and an array of coefficients (in case I get
something like 2x*2y). I have no problems with the constructor or
anything else, but I can't figure out how to make the constructor
accept an array as an argument without resorting to arbitrary array
creation and then deleting the array later.

I'm very confused. An array is just another type, and there's no arbitrary
creation needed. There's no deleting either, of course - unused objects get
garbage collected eventually, which is true of arrays just like any type.

Are you talking about the caller? You don't like having to do
someMethod(new Term[] { term1, term2, term3 });
because you think it's a waste to new up an array?

1) Get over it, unless you have actual profiles showing it to be a problem.
object creation is cheap, including (small) array creation.

2) The jdk1.5 vararg syntax makes this feel a bit nicer, as you can declare
public void someMethod(Term foo...) { ... }
and call it as
someMethod(term1, term2, term3);
This will create an array under the covers, so there's no performance
difference, just a convenience to the programmer.

3) If you're taking multiple arrays, say of variables, exponents, and
coefficients, please consider making a class that consists of one of each,
and passing just an array of that type. It's way cleaner.
 
J

jupiter

I have this class Term (of the algebraic kind) and I want it to
accept
an array of variables and an array of coefficients (in case I get
something like 2x*2y). I have no problems with the constructor or
anything else, but I can't figure out how to make the constructor
accept an array as an argument without resorting to arbitrary
array
creation and then deleting the array later. It feels unnecessary.

The constructor isn't creating the arrays if it's receiving them as
parameters. Those are references to the objects (arrays) on the
heap.

There is no need to manually delete (set to null) an array because
it gets removed from the heap automatically when it goes out of
scope. But there is nothing wrong with setting an object to null.
It just isn't something to worry about. If your constructor
constructs properly then you have what you need.
 
H

Hakusa

OK, time for a more specific example of what I'm trying to do:

I've already created classes for a variable (which includes a base
(String) and int exponent). I'm now creating a class called Term that
contains the variable and coefficient classes so I can do different
algebraic, although simple, stuff. Later I want to make an expressions
class to do even more things.

Right now, I'm doing trying to instantiate the Terms class. The
following lines of code that were partially inspired by the solutions
I got here. The following lines of code work. Thank you.

Variable var1 = new Variable("x");
Term term1 = new Term( new int[] {2}, new Variable[] {var1} );
 
M

Mark Rafn

I've already created classes for a variable (which includes a base
(String) and int exponent).

This makes little sense. A Variable should not have an exponent, it should
have exponents applied to it in various equations or operations. In the case
of 4x*x - 3x + 12 = 0, the Variable is "x". It has two different uses, with
different exponents.
I'm now creating a class called Term that contains the variable and
coefficient classes so I can do different algebraic, although simple, stuff.

I think you want a Term to have the exponent in addition to the coefficient.
Right now, I'm doing trying to instantiate the Terms class. The
following lines of code that were partially inspired by the solutions
I got here. The following lines of code work. Thank you.

Variable var1 = new Variable("x");
Term term1 = new Term( new int[] {2}, new Variable[] {var1} );

You're overcomplexifying by trying to avoid designing a class structure.
Don't do that - take the time to think about your object model, rather than
passing around arrays of primitives that the caller needs to build and keep in
sync with each other. Consider:
interface Term
Constant implements Term (a constant value)
Variable implements Term (a simple variable to be solved for or evaluated)
Operator (an operator, like exponentiation, division, sin, etc.)
Expression implements Term (Operator operator, Term... operands)
Equation (Term, Term)
Relation (equals, greater than, etc.)
Inequality (Term, Relation, Term)
With a sufficient set of Operators, you can encode just about anything you want.
You may want convenience subclasses of CompoundTerm, like
PolynomialTerm (Constant coefficient, Term base, Constant exponent)
and since Constant has constructors for double and long, and Variable has a
String constructor, you can have convenience constructors on PolynomialTerm
PolynomialTerm(double coefficient, String variable, double exponent)
and so on.

You'll end up being able to do something, after creating enough constructors
to do the right thing with strings and typed parameters, like
Variable x = new Variable("x");
Variable y = new Variable("y");
new Inequality(
new Expression("+", new PolynomialTerm(4, x, 2),
new PolynomialTerm(7, new Expression("*", x, y), 1),
new PolynomialTerm(-14, y, 2)),
Relation.valueOf(">="),
Constant.PI);

Which is "4x^2 + 7xy -14y^2 >= PI".

It's also fairly amenable to constructing things as you parse strings.
 
H

Hakusa

I've already created classes for a variable (which includes a base
(String) and int exponent).

This makes little sense. A Variable should not have an exponent, it should
have exponents applied to it in various equations or operations. In the case
of 4x*x - 3x + 12 = 0, the Variable is "x". It has two different uses, with
different exponents.
I'm now creating a class called Term that contains the variable and
coefficient classes so I can do different algebraic, although simple, stuff.

I think you want a Term to have the exponent in addition to the coefficient.
Right now, I'm doing trying to instantiate the Terms class. The
following lines of code that were partially inspired by the solutions
I got here. The following lines of code work. Thank you.
Variable var1 = new Variable("x");
Term term1 = new Term( new int[] {2}, new Variable[] {var1} );

You're overcomplexifying by trying to avoid designing a class structure.
Don't do that - take the time to think about your object model, rather than
passing around arrays of primitives that the caller needs to build and keep in
sync with each other. Consider:
interface Term
Constant implements Term (a constant value)
Variable implements Term (a simple variable to be solved for or evaluated)
Operator (an operator, like exponentiation, division, sin, etc.)
Expression implements Term (Operator operator, Term... operands)
Equation (Term, Term)
Relation (equals, greater than, etc.)
Inequality (Term, Relation, Term)
With a sufficient set of Operators, you can encode just about anything you want.
You may want convenience subclasses of CompoundTerm, like
PolynomialTerm (Constant coefficient, Term base, Constant exponent)
and since Constant has constructors for double and long, and Variable has a
String constructor, you can have convenience constructors on PolynomialTerm
PolynomialTerm(double coefficient, String variable, double exponent)
and so on.

You'll end up being able to do something, after creating enough constructors
to do the right thing with strings and typed parameters, like
Variable x = new Variable("x");
Variable y = new Variable("y");
new Inequality(
new Expression("+", new PolynomialTerm(4, x, 2),
new PolynomialTerm(7, new Expression("*", x, y), 1),
new PolynomialTerm(-14, y, 2)),
Relation.valueOf(">="),
Constant.PI);

Which is "4x^2 + 7xy -14y^2 >= PI".

It's also fairly amenable to constructing things as you parse strings.

I'm still relatively new to Java, so I only think I understood what
you said, but there's a reason I want Tern to be a class and not
implementable interface. A variable has a base and an exponent.
Coefficients aren't involved in the same processes as so I only want
them to be part of the Term class. The Term class can accept multiple
coefficients, but automatically simplifies them to one. The big idea
is that the computer could eventually do the following piece of
algebra.

5x^2*4b (the computer multiplies them together
20bx^2

And later I could have it catch up with my Algebra class at school.
But because all of that was all one term, I want to treat it as one
object for when I get expressions in there.

I hope this has explained it enough so that you understand or can tell
me why I'm wrong. Thank you.
 
O

Oliver Wong

OK, time for a more specific example of what I'm trying to do:

I've already created classes for a variable (which includes a base
(String) and int exponent). I'm now creating a class called Term that
contains the variable and coefficient classes so I can do different
algebraic, although simple, stuff. Later I want to make an expressions
class to do even more things.

Right now, I'm doing trying to instantiate the Terms class. The
following lines of code that were partially inspired by the solutions
I got here. The following lines of code work. Thank you.

Variable var1 = new Variable("x");
Term term1 = new Term( new int[] {2}, new Variable[] {var1} );

This is a typical exercise when learning about parsing and compiler
construction, so you might be interested in doing some reading on compiler
theory to see how others have solved the problem in the past, and whether
you might be able to reuse their ideas in your solution.

I don't have any specific recommendations (everybody seems to recommend
the so called "Dragon Book", but I haven't read it myself, having learnt
this stuff only through my professor's proprietary notes), but you can get
started at the Wikipedia article http://en.wikipedia.org/wiki/Parsing and in
particular, look at the diagram at
http://en.wikipedia.org/wiki/Image:Parsing-example.png

- Oliver
 
A

Andy Dingley

I don't have any specific recommendations (everybody seems to recommend
the so called "Dragon Book", but I haven't read it myself,

Anything in 2006 that really did require me to dust off my Dragon Book
would also give me The Fear. It's a great book and everyone ought to
read it sometime, but to actually _need_ it these days for any
"typical" commercial coding problem makes me suspect that something
about the intended solution is bizarrely over-complicating things.
 
M

Mark Rafn

I'm still relatively new to Java, so I only think I understood what
you said, but there's a reason I want Tern to be a class and not
implementable interface.

Cool. It's your code, you know best what you want out of it.
A variable has a base and an exponent.

Not in any algebra I've used. A variable is a variable, and is combined with
coefficients and exponents to make an expression, which can be used in
equations and inequalities or evaluated for given values of the variable.
Picking nonstandard definitions of common words is usually a bad idea in class
design.
5x^2*4b (the computer multiplies them together
20bx^2

Pick a more complicated example.
(x-1)(x^2+1). This should become x^3 - x^2 + x - 1. Your evaluator will need
to know that "x" is a variable, and that it's the SAME variable with different
exponents applied in various places.
I hope this has explained it enough so that you understand or can tell
me why I'm wrong. Thank you.

I suspect you're going to dead end pretty quickly if you don't start with
standard mathematical concepts (variable, constant, operation) and work up
from there. It all depends on where you're going with it, though, and you
get to choose your own path.
 
L

Lew

Andy said:
Anything in 2006 that really did require me to dust off my Dragon Book
would also give me The Fear. It's a great book and everyone ought to
read it sometime, but to actually _need_ it these days for any
"typical" commercial coding problem makes me suspect that something
about the intended solution is bizarrely over-complicating things.

Perhaps one doesn't need a full compiler, but LALR and recursive-descent
parsing is an incredibly useful tool for all kinds of applications.

Essentially, if you can express a data-processing task in EBNF, you can render
each "reserved word" of your grammar as a class or a method, and thus create a
threaded, single-pass processor. Some years back I used this approach to
create transformation routines between EDI messages and database transactions.
Even with Java 1.1 it could process something like 200,000 documents an hour
over a TCP connection.

- Lew
 
A

Andy Dingley

Essentially, if you can express a data-processing task in EBNF, you can render
each "reserved word" of your grammar as a class or a method, and thus create a
threaded, single-pass processor.

I already do something akin to this, I just do it very inefficiently
(with XSLT, and assuming an input that's already XML and thus
trivially parsed)

My problems these days are rarely about execution speed and far more
often about implementation speed. "Just about good enough" always
wins, so long as I can ship it yesterday. Certainly there's no reward
for a "good" solution because most businesses just don't have the
ability to recognise one.

(I'm not entirely happy about this, but it's where the jobs are).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top