I'm trying to upgrade my class library to my current mathematical skill set and am having trouble with multi-term polynomials, especially those I where I don't know how many terms the user will be putting in. I know that the formula for the first derivative of a polynomial is ax^n = n * ax^n-1; and the second derivative is the derivative of the first derivative. So for example:

Find the derivative of 4x^2:
4x^2 = 2 * 4x^2 - 1 = 8x^2-1 = 8x

Find the second derivative of 4x^3:
4x^3 = 3 * 4x^3-1 = 12x^3-1 = 12x^2
12x^2 = 2 * 12x^2-1 = 24x^2-1 = 24x

Right now I just have an algorithm for the first derivative of a single term polynomial:

// Pseudocode (C# Syntax):
public string DerivativeOfPolynomial(int Base, string Variable, int Exponent) {
    Base = Exponent * Base;
    Exponent = Exponent > 1 ? Exponent - 1 : 0;
    Variable = Exponent == 0 ? "" : Variable;

    return Base.ToString() + Variable + (Exponent == 0 ? "" : Exponent).ToString();
}

Chose to not work on the second derivatives until I figure out the whole parameter problem. I'm sure I have to use arrays or lists to accomplish this, but I'm not sure how the algorithm would work for a polynomial with 3 terms:

3x^2 + 13x + 4

Also the derivative of a constant (basically any number without a variable and exponent of 2 or higher) is equal to 0 for those who don't know.

Recommended Answers

All 6 Replies

Dot net collections are good for unknown amounts of something (like a list).
You can even make an extension method on the list to process the elements throught he functions.

For example? LOL

I knew that I should use a list, but the problem is how do I read each one as part of each polynomial:

// Pseudocode:
private string DerivativeOfPolynomial(int[] Bases, string[] Variables, int[] Exponents) {
    string temp = "";
    for (int i = 0; i < Bases.Length; i++) {
        Bases *= Exponents[i] >= 1 ? Exponents[i] : 1;
        Exponents[i] -= Exponents[i] > 1 ? 1 : 0;
        string exTemp = Exponents[i] == 0 ? "" : Exponents[i].ToString();
        Variables[i] = Exponents[i] > 1 ? Variables[i] + "^" : "";

        temp += Bases.ToString() + Variables[i] + exTemp + " :: ";
    }

    return temp;
}

However if all three array lengths are not the same either an exception will be thrown for index out of bounds or not all terms will be simplified.

Why not use a class to hold the exponent/variables/bases?

class Term
{
    public int Base {get; set;}
    public string Variable {get; set;}
    public int Exponent {get; set;}
}
private string DerivativeOfPolynomial(IList<Term> terms)
{
    return  string.Join(" ",
        from term in terms
        let tempExponent = term.Exponent - (term.Exponent > 1 ? 1 : 0)
        select ((term.Base * (term.Base >= 1 ? term.Exponent : 1)).ToString() + 
        (tempExponent > 1 ? term.Variable + "^" : "") +
        (tempExponent > 0 ? tempExponent.ToString() : "") + " :: ").ToList());        
}

Edit: I redid your solution with Linq before Thines could lol!

LOL @skatamatic I was just waiting on your reply, and the people over in the C++ section said to use a struct. But I couldn't "completely" understand the sample they gave because I'm still a little new to C++. However, I like the example you gave, and can at least understand it partially. Could you do me a solid and explain how the 'from' structure you have there is working? I don't completely think I understand the 'let' and select' keywords. From just reading the example it looks like it's creating a new temporary variable called 'tempExponent' then setting the value to the exponent minus the result of a conditional (one or zero). Then as for the select keyword, I have now idea what it is doing. Also, is the structure compatible with C++? Thanks again for the help.

I can't remember if C++ supports automatic properties, and it is using a string reference rather than a cstring or character array but it should be pretty transferable to C++ as a class or a struct.

As for the 'from', 'in', 'let' and 'select', you have just seen Linq in action :) I'm not going to explain it much (it's practically a language of its own) but it allows for very syntactically clear and dynamic iteration of collections. The basic idea was to integrate SQL-like queries directly into C# syntax, and allow it to interact with objects. It has grown to support quite a bit and is probably worth your time to look into. Here's a good link. And some examples.

Thanks, those where very helpful. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.