Contributor Challenge: Polynomial math

by sonone, Dec 1, 2020, 11:44 AM

Hello folks!
For a contributor challenge (admin if you already are a contributor), you will have to make an Asymptote code that allows you to
  • make polynomials
  • label them (in LaTeX)
  • add them
  • subtract them
  • multiply them
If you want to go for automatic admin, you can add dividing polynomials too.
Since this is relatively hard, I have provided a few tips: Tip 1. Tip 2. Tip 3.

Good luck!

Comment

13 Comments

The post below has been deleted. Click to close.
This post has been deleted. Click here to see post.
So there should be only 1 label that outputs the results?

by TheFlamingoHacker, Dec 1, 2020, 7:05 PM

The post below has been deleted. Click to close.
This post has been deleted. Click here to see post.
Yes, if you mean you can only use the label function once.

by sonone, Dec 1, 2020, 9:02 PM

The post below has been deleted. Click to close.
This post has been deleted. Click here to see post.
Store then as a string I assume... ?

by TheFlamingoHacker, Dec 2, 2020, 3:46 AM

The post below has been deleted. Click to close.
This post has been deleted. Click here to see post.
Yes, you have the idea!

by sonone, Dec 2, 2020, 10:46 AM

The post below has been deleted. Click to close.
This post has been deleted. Click here to see post.
Oof I know how to do structures but I am bad at math :(

by player01, Dec 2, 2020, 11:46 PM

The post below has been deleted. Click to close.
This post has been deleted. Click here to see post.
I'm interested in doing this challenge, but I have finals coming up soon. :(

by SirCalcsALot, Dec 3, 2020, 5:45 AM

The post below has been deleted. Click to close.
This post has been deleted. Click here to see post.
I’ll try this. :D

by TheFlamingoHacker, Dec 3, 2020, 7:35 AM

The post below has been deleted. Click to close.
This post has been deleted. Click here to see post.
OK, so the class has to take in $n$ arguments right (because it is a polynomial)

by player01, Dec 3, 2020, 4:41 PM

The post below has been deleted. Click to close.
This post has been deleted. Click here to see post.
I've heard about structures but I don't know what they are. Can you explain?

by TheFlamingoHacker, Dec 3, 2020, 9:30 PM

The post below has been deleted. Click to close.
This post has been deleted. Click here to see post.
Go here: https://asymptote.sourceforge.io and click Documentation and then click Structures.

by player01, Dec 7, 2020, 10:23 PM

The post below has been deleted. Click to close.
This post has been deleted. Click here to see post.
Here is my code: Code

[asy]
struct poly
{
	real[] coeff;
    int length;
    
    void operator init(... real[] coeff)
    {
    	this.coeff = coeff;
        this.length = coeff.length;
    }
    
    string _str(real s)
    {
    	if (s < 0)
        {
        	return "(" + string(s) + ")";
        }
        return string(s);
    }
    
    string getstr()
    {
    	string res = "";

        for (int n = 0; n < this.coeff.length; ++n)
        {
        	if (this.coeff[n] != 0)
            {
            	if (this.coeff.length - n - 1 == 0)
                {
                	res += this._str(this.coeff[n]);
                }
                else if (this.coeff.length - n - 1 == 1)
                {
                	res += this._str(this.coeff[n]) + "x";
                }
                else
                {
            		res += this._str(this.coeff[n]) + "x^" + "{" + string(this.coeff.length - n - 1) + "}";
                }
            }
            else
            {
            	res = reverse(substr(reverse(res), 1));
            }

            if (n != this.coeff.length - 1)
            {
            	res += "+";
            }
        }
        return "$" + res + "$";
    }
}

poly operator + (poly first, poly sec)
{
    real[] newpoly;
    
    real[] left;
    
    real[] a, b;
    
    if (first.length > sec.length)
    {
    	left = first.coeff[:first.length - sec.length];
        a = first.coeff[first.length - sec.length:];
        b = sec.coeff;
    }
    else if (first.length < sec.length)
    {
		left = sec.coeff[:sec.length - first.length];
        a = sec.coeff[sec.length - first.length:];
        b = first.coeff;
	}
    else
    {
    	a = first.coeff;
        b = sec.coeff;
    }
    newpoly.append(left);
    
    for (int n = 0; n < a.length; ++n)
    {
    	newpoly.push(a[n] + b[n]);
    }
    
    return poly(... newpoly);
}

real negate(real x) {return -x;}

poly operator - (poly first, poly sec)
{
    return first + poly(... map(negate, sec.coeff));
}

poly operator * (poly first, poly sec)
{
	real[] coeff;
	for (int x = 0; x < first.length; ++x)
    {
    	for (int y = 0; y < sec.length; ++y)
        {
        	coeff.push(first.coeff[x] * sec.coeff[y]);
        }
    }
    
    return poly(... coeff);
}

poly test = poly(4, 2);
poly test2 = poly(3, -4);

poly added = test * test2;
label(added.getstr());
[/asy]

by player01, Mar 11, 2021, 3:52 AM

The post below has been deleted. Click to close.
This post has been deleted. Click here to see post.
I haven't tested it a ton yet, but it seems to work :) (sry for the triple post)

by player01, Mar 11, 2021, 3:53 AM

The post below has been deleted. Click to close.
This post has been deleted. Click here to see post.
Congratulations!

by sonone, May 18, 2021, 1:18 PM

Old material is mostly Asymptote, new material is calculator programming

avatar

sonone
Archives
+ April 2023
+ August 2022
+ April 2021
+ August 2020
Shouts
Submit
  • I still exist as well.

    by G.G.Otto, Aug 11, 2023, 2:44 AM

  • hello I'm still here lol

    by player01, Aug 6, 2022, 6:24 PM

  • [REVIVAL] I will start posting more calculator relating posts very soon. Even though school has been busy, I have been programming my calculators a decent amount, so I have a lot to share...

    by sonone, Feb 18, 2022, 10:29 PM

  • wow its been like 2.5 years since geo class

    by pieMax2713, Feb 4, 2022, 8:38 PM

  • @violin21, I've been very busy with school lately and haven't been able to add another lesson. I will when i get a free moment

    by sonone, Aug 19, 2021, 12:45 AM

  • ORZ CODER

    by samrocksnature, Aug 9, 2021, 9:57 PM

  • Could you make more Asymptote lessons on your "How to do Asymptote" blog?

    by violin21, Aug 9, 2021, 7:26 PM

  • You can take it, just C&P the CSS into your CSS area

    by sonone, Apr 17, 2021, 10:08 PM

  • how can we take the CSS if we have permission to not take it?

    by GoogleNebula, Apr 17, 2021, 5:22 PM

  • That is awesome!

    by sonone, Apr 15, 2021, 10:09 PM

  • I modified your dodecahedron and got:
    [asy]
    import three;
    import solids;
    size(300);
    currentprojection=orthographic(0,1.3,1.2);
    light(0,5,10);

    real phi=(sqrt(6)+1)/3;
    real g=(phi-1)/2;
    real s=1/2;
    real a=sqrt(1-phi*phi/4-g*g)+phi/2;

    triple[] d;
    d[0]=(phi

    by Andrew2019, Mar 26, 2021, 12:15 AM

  • Not too many, just changing the color here and there. I really like your CSS!

    by sonone, Feb 2, 2021, 10:35 AM

  • Nice!

    I see you're making changes to the CSS. :)

    by G.G.Otto, Feb 1, 2021, 9:26 PM

  • I'm learning Java now!

    by sonone, Feb 1, 2021, 5:56 PM

  • And I took part of it from CaptainFlint and then added a ton of modifications. ;)

    by G.G.Otto, Dec 1, 2020, 8:56 AM

98 shouts
Tags
About Owner
  • Posts: 2106
  • Joined: Aug 20, 2016
Blog Stats
  • Blog created: Mar 28, 2020
  • Total entries: 61
  • Total visits: 4961
  • Total comments: 146
Search Blog