Im starting a program that is supposed to take in polynomials from the user.
From there they can add them, subract, multiply, or divide them.
My question is more of the design. What would be a good way to design it
be? I have a polynomial class and the only way Ive figured out to do this
is put in add, subtract etc functions into the polynomial class and pass one
polynomial to the other. But that doesnt seem very good. Seems inefficient
somehow.
I hope I explained that enough for people to understand. I did some C back
in college about 5 years ago but have spent my professinal career doing ASP
and net languages which drive me crazy. Trying to get back into something
more real...
Thanks 9 1565
Ross Olney wrote: Im starting a program that is supposed to take in polynomials from the user. From there they can add them, subract, multiply, or divide them.
My question is more of the design. What would be a good way to design it be? I have a polynomial class and the only way Ive figured out to do this is put in add, subtract etc functions into the polynomial class and pass one polynomial to the other. But that doesnt seem very good. Seems inefficient somehow.
Why do you think it is inefficient ?
Or, more importantly, why are you concerned about efficiency ? I hope I explained that enough for people to understand. I did some C back in college about 5 years ago but have spent my professinal career doing ASP and net languages which drive me crazy. Trying to get back into something more real...
You could use a non-member operator function - e.g.
template <typename T=double>
class Poly;
template <typename T=double>
Poly<T> operator+( const Poly<T> &, const Poly<T> & );
template <typename T=double>
Poly<T> operator-( const Poly<T> &, const Poly<T> & );
template <typename T=double>
Poly<T> operator*( const Poly<T> &, const Poly<T> & );
template <typename T=double>
Poly<T> operator/( const Poly<T> &, const Poly<T> & );
template <typename T=double>
Poly<T> operator*( const Poly<T> &, const T & );
template <typename T=double>
Poly<T> operator*( const T &, const Poly<T> & );
This way, you could write code like:
Poly< complex<double> > a, b;
....
b = 3 * a;
BTW - dividing polynomials to produce a polynomial will be an
interesting trick.
* Ross Olney: Im starting a program that is supposed to take in polynomials from the user. From there they can add them, subract, multiply, or divide them.
My question is more of the design. What would be a good way to design it be? I have a polynomial class and the only way Ive figured out to do this is put in add, subtract etc functions into the polynomial class and pass one polynomial to the other. But that doesnt seem very good. Seems inefficient somehow.
For efficiency you might consider implementing Polynomial as a
smart-pointer like class, each object containing a refcounted pointer to
But first check whether you really need that, by measuring.
The code below exemplifies how to do the add, subtract etc. regardless
of whether the internal representation is direct or smartpointer-like:
#include <iostream>
class Integer
{
private:
int myValue;
public:
Integer( int aValue = 0 ): myValue( aValue ) {}
int const value() const { return myValue; }
Integer& operator+=( Integer const& rhs )
{
myValue += rhs.value();
return *this;
}
};
inline Integer operator+( Integer const& a, Integer const& b )
{
Integer result = a;
return result += b;
}
int main()
{
Integer a = 3;
Integer b = 5;
std::cout << (a+b).value() << std::endl;
}
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
"Ross Olney" <rm*****@adelphia.net> wrote in message
news:Ja********************@adelphia.com... Im starting a program that is supposed to take in polynomials from the user. From there they can add them, subract, multiply, or divide them.
My question is more of the design. What would be a good way to design it be? I have a polynomial class and the only way Ive figured out to do this is put in add, subtract etc functions into the polynomial class and pass one polynomial to the other. But that doesnt seem very good. Seems inefficient somehow.
I hope I explained that enough for people to understand. I did some C back in college about 5 years ago but have spent my professinal career doing ASP and net languages which drive me crazy. Trying to get back into something more real...
Thanks
Can your polynomials be really big?
Yeah sure. Ideally Id like to get it so they can enter in as many terms as
they want. For starters I might stick with just a standard 3 term poly
though.
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:41***********************@news.optusnet.com.a u... "Ross Olney" <rm*****@adelphia.net> wrote in message news:Ja********************@adelphia.com... Im starting a program that is supposed to take in polynomials from the user. From there they can add them, subract, multiply, or divide them.
My question is more of the design. What would be a good way to design it be? I have a polynomial class and the only way Ive figured out to do this is put in add, subtract etc functions into the polynomial class and pass one polynomial to the other. But that doesnt seem very good. Seems inefficient somehow.
I hope I explained that enough for people to understand. I did some C back in college about 5 years ago but have spent my professinal career doing ASP and net languages which drive me crazy. Trying to get back into something more real...
Thanks
Can your polynomials be really big?
somehow it just seemed like one object being passed to another object of the
same type wasn't quite right. I guess something about having the passed
object contain all these same functions that it wont ever use is wasteful
somehow. From a performance perspective I dont care at all. I just want to
be sure Im designing it right and not getting into bad habits
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:8c********************@speakeasy.net... Ross Olney wrote: Im starting a program that is supposed to take in polynomials from the user. From there they can add them, subract, multiply, or divide them.
My question is more of the design. What would be a good way to design it be? I have a polynomial class and the only way Ive figured out to do this is put in add, subtract etc functions into the polynomial class and pass one polynomial to the other. But that doesnt seem very good. Seems inefficient somehow.
Why do you think it is inefficient ?
Or, more importantly, why are you concerned about efficiency ?
I hope I explained that enough for people to understand. I did some C back in college about 5 years ago but have spent my professinal career doing ASP and net languages which drive me crazy. Trying to get back into something more real...
You could use a non-member operator function - e.g.
template <typename T=double> class Poly;
template <typename T=double> Poly<T> operator+( const Poly<T> &, const Poly<T> & );
template <typename T=double> Poly<T> operator-( const Poly<T> &, const Poly<T> & );
template <typename T=double> Poly<T> operator*( const Poly<T> &, const Poly<T> & );
template <typename T=double> Poly<T> operator/( const Poly<T> &, const Poly<T> & );
template <typename T=double> Poly<T> operator*( const Poly<T> &, const T & );
template <typename T=double> Poly<T> operator*( const T &, const Poly<T> & );
This way, you could write code like:
Poly< complex<double> > a, b;
.... b = 3 * a;
BTW - dividing polynomials to produce a polynomial will be an interesting trick.
"Rv5" <rm*****@adelphia.net> wrote in message
news:2L********************@adelphia.com... somehow it just seemed like one object being passed to another object of the same type wasn't quite right. I guess something about having the passed object contain all these same functions that it wont ever use is wasteful somehow. From a performance perspective I dont care at all. I just want to be sure Im designing it right and not getting into bad habits
If it worries you that a polynomial object doesn't use one of its member
functions then take that function out of the Polynomial class altogether and
put it in another class, PolynomialOperations say. This new class can do the
work of adding and multiplying polynomials for you.
class PolynomialOperations
{
public:
Polynomial add(Polynomial x, Polynomial y) const { /* ... */ }
Polynomial multiply(Polynomial x, Polynomial y) const { /* ... */ }
};
Then for convenience you can define global functions that overload + and *
as follows:
Polynomial operator+(Polynomial x, Polynomial y)
{ return PolynomialOperations().add(x, y); }
Polynomial operator*(Polynomial x, Polynomial y)
{ return PolynomialOperations().multiply(x, y); }
You might find this design more attactive than one that defines functions
for addition and multiplication as members of the Polynomial class itself.
Jason, thats great, thanks. Ill try that. Didnt think of it quite like
that.
Thanks again
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:41***********************@news.optusnet.com.a u... "Rv5" <rm*****@adelphia.net> wrote in message news:2L********************@adelphia.com... somehow it just seemed like one object being passed to another object of the same type wasn't quite right. I guess something about having the passed object contain all these same functions that it wont ever use is wasteful somehow. From a performance perspective I dont care at all. I just want to be sure Im designing it right and not getting into bad habits
If it worries you that a polynomial object doesn't use one of its member functions then take that function out of the Polynomial class altogether and put it in another class, PolynomialOperations say. This new class can do the work of adding and multiplying polynomials for you.
class PolynomialOperations { public: Polynomial add(Polynomial x, Polynomial y) const { /* ... */ } Polynomial multiply(Polynomial x, Polynomial y) const { /* ... */ } };
Then for convenience you can define global functions that overload + and * as follows:
Polynomial operator+(Polynomial x, Polynomial y) { return PolynomialOperations().add(x, y); }
Polynomial operator*(Polynomial x, Polynomial y) { return PolynomialOperations().multiply(x, y); }
You might find this design more attactive than one that defines functions for addition and multiplication as members of the Polynomial class itself.
"Rv5" <rm*****@adelphia.net> wrote in message
news:ef********************@adelphia.com... "Jason Heyes" <ja********@optusnet.com.au> wrote in message news:41***********************@news.optusnet.com.a u... Can your polynomials be really big? Yeah sure. Ideally Id like to get it so they can enter in as many terms as they want. For starters I might stick with just a standard 3 term poly though.
It sounds like a good programming exercise. Support polynomials of any
degree from the start. You won't find life any easier limiting the size of
your polynomials. Use a resizable array such as std::vector to hold your
coefficients. You'd be surprised how easy it is to use.
"Ross Olney" <rm*****@adelphia.net> skrev i en meddelelse
news:Ja********************@adelphia.com... Im starting a program that is supposed to take in polynomials from the user. From there they can add them, subract, multiply, or divide them.
My question is more of the design. What would be a good way to design it be? I have a polynomial class and the only way Ive figured out to do this is put in add, subtract etc functions into the polynomial class and pass one polynomial to the other. But that doesnt seem very good. Seems inefficient somehow.
I hope I explained that enough for people to understand. I did some C back in college about 5 years ago but have spent my professinal career doing ASP and net languages which drive me crazy. Trying to get back into something more real...
Thanks
Do it like this:
class polynomial
{
public:
polynomial& operator +=(const polynomial &rhs);
// same with *= -= /= and =
};
polynomial operator+(polynomial lhs,polynomial const& rhs)
// same with operator -,*,/
{
return lhs += rhs;
}
/Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Geoff Biggs |
last post by:
Evening all,
I'm trying to add a new built-in number data type to Python with its own
syntax, so I'm working directly with the interpreter rather than
creating my own extension module (side...
|
by: cppaddict |
last post by:
Hi,
I have some code which uses std::map to associate cartesian POINTs
with values. My current syntax for adding new (POINT,value) entries
to the map is clunky -- it takes three lines for each...
|
by: Steven D'Aprano |
last post by:
Suppose I create a class with some methods:
py> class C:
.... def spam(self, x):
.... print "spam " * x
.... def ham(self, x):
.... print "ham * %s" % x
.......
|
by: Troy |
last post by:
Hello,
I have a dumb question. I have two array objects of type double with 12
elements in each object. I'd like to add the two objects but dont know how
to. Any ideas?
thanks
|
by: MIGUEL |
last post by:
Hi all!
Be patient because what I'm going to explain all of you it's more than
very strange.
I've developed a webservice project that contains two classes. One of
them is going to act as a...
|
by: Pierre Barbier de Reuille |
last post by:
Please, note that I am entirely open for every points on this proposal
(which I do not dare yet to call PEP).
Abstract
========
This proposal suggests to add symbols into Python.
Symbols...
|
by: Albert |
last post by:
So structures are useful to group variables, so you can to refer to a
collection as a single entity. Wouldn't it be useful to also have the
ability to collect variable and functions?
Ask K&R...
|
by: silversurfer |
last post by:
Hello,
I am a little unsure whether this method really makes sense. The goal
is to add an element to a vector. This is the struct and method I am
using:
std::vector<Entry> models;
struct...
|
by: =?Utf-8?B?TGVvbiBNYXluZQ==?= |
last post by:
In a database project in VS2005 (not a datadude project), I need to add about
3,000 stored procedure scripts to my 'Stored Procedures' folder. I've dropped
the scripts into the folder in my...
|
by: Maximus Decimus |
last post by:
HI all,
I am using python v2.5 and I am an amateur working on python. I am
extending python for my research work and would like some help and
guidance w.r.t this matter from you experienced...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |