Unicode polynomial equation: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: syntax coloured, made p2js compatible)
m (syntax highlighting fixup automation)
Line 60: Line 60:
=={{header|Go}}==
=={{header|Go}}==
Although this program provides full support for Unicode vulgar fractions, note that there is no guarantee that they (or arithmetic on them) will successfully 'round trip' due to floating point arithmetic being used in the underlying calculations and some of them being recurring rather than exact decimals in any case.
Although this program provides full support for Unicode vulgar fractions, note that there is no guarantee that they (or arithmetic on them) will successfully 'round trip' due to floating point arithmetic being used in the underlying calculations and some of them being recurring rather than exact decimals in any case.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 316: Line 316:
printEquation(coefs)
printEquation(coefs)
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 371: Line 371:
=={{header|Julia}}==
=={{header|Julia}}==
The task allows the "polynomials" to be parsed to have negative exponents. This makes them <em>Laurent</em> polynomials, not ordinary polynomials.
The task allows the "polynomials" to be parsed to have negative exponents. This makes them <em>Laurent</em> polynomials, not ordinary polynomials.
<lang julia>import Base.print
<syntaxhighlight lang="julia">import Base.print


struct LaurentPolynomial{T}
struct LaurentPolynomial{T}
Line 505: Line 505:
println(lpad(s, 48), " => ", topoly(s, 'x'))
println(lpad(s, 48), " => ", topoly(s, 'x'))
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
1x⁵ - 2x⁴ + 42x³ + 0x² + 40x + 1 => x⁵ - 2x⁴ + 42x³ + 40x + 1
1x⁵ - 2x⁴ + 42x³ + 0x² + 40x + 1 => x⁵ - 2x⁴ + 42x³ + 40x + 1
Line 524: Line 524:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Go}}
{{trans|Go}}
<lang Nim>import re, sequtils, strutils, tables
<syntaxhighlight lang="nim">import re, sequtils, strutils, tables
from unicode import toRunes
from unicode import toRunes


Line 675: Line 675:
if c != 0: coefs[p] = coefs.getOrDefault(p) + c
if c != 0: coefs[p] = coefs.getOrDefault(p) + c


printEquation(coefs)</lang>
printEquation(coefs)</syntaxhighlight>


{{out}}
{{out}}
Line 728: Line 728:
=={{header|Phix}}==
=={{header|Phix}}==
To simplify this task I first created a test file (save as utf8, Unicode_polynomial_equation.e):
To simplify this task I first created a test file (save as utf8, Unicode_polynomial_equation.e):
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">raw_text</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
<span style="color: #008080;">constant</span> <span style="color: #000000;">raw_text</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
-0.00x⁺¹⁰ + 1.0·x ** 5 + -2e0x^4 + +0,042.00 × x ⁺³ + +.0x² + 20.000 000 000x¹ - -1x⁺⁰ + .0x⁻¹ + 20.x¹
-0.00x⁺¹⁰ + 1.0·x ** 5 + -2e0x^4 + +0,042.00 × x ⁺³ + +.0x² + 20.000 000 000x¹ - -1x⁺⁰ + .0x⁻¹ + 20.x¹
Line 773: Line 773:
<span style="color: #008080;">return</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">raw_text</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">raw_text</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
Explanation: <br>
Explanation: <br>
On finding a line beginning with "==> ", parse the previous line and check that the output matches the rest of the line.<br>
On finding a line beginning with "==> ", parse the previous line and check that the output matches the rest of the line.<br>
Line 790: Line 790:
bytes happen to be less than 255, which would not necessarily be valid utf8/32.
bytes happen to be less than 255, which would not necessarily be valid utf8/32.


<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Unicode_polynomial_equation.exw</span>
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Unicode_polynomial_equation.exw</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">uni_frac</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">WINDOWS</span> <span style="color: #000080;font-style:italic;">-- if true output unicode superscripts and vulgar fractions</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">uni_frac</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">WINDOWS</span> <span style="color: #000080;font-style:italic;">-- if true output unicode superscripts and vulgar fractions</span>
Line 1,132: Line 1,132:
<span style="color: #0000FF;">?</span><span style="color: #008000;">"done"</span>
<span style="color: #0000FF;">?</span><span style="color: #008000;">"done"</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
uni_frac = false (ie windows)
uni_frac = false (ie windows)
Line 1,180: Line 1,180:
{{trans|Go}}
{{trans|Go}}
{{libheader|Wren-pattern}}
{{libheader|Wren-pattern}}
<lang ecmascript>import "/pattern" for Pattern
<syntaxhighlight lang="ecmascript">import "/pattern" for Pattern


var powers = [
var powers = [
Line 1,396: Line 1,396:
}
}
printEquation.call(coefs)
printEquation.call(coefs)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}