This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
KingofGamesYami's profile picture

[REQUEST]Synthetic Division in CC Lua

Started by KingofGamesYami, 22 October 2015 - 01:46 PM
KingofGamesYami #1
Posted 22 October 2015 - 03:46 PM
I've been trying to do this for 3 or 4 days now, and it's stalling my current project. I would like a function which, when passed a table of arguments in the format [power] = coefficient and a number, it will use synthetic division to create a new table in the same format, which it returns.

If you don't know what Synthetic Division is, you can learn more about it here.
Bomb Bloke #2
Posted 23 October 2015 - 03:14 AM
Can you provide sample input/output for this function?
KingofGamesYami #3
Posted 23 October 2015 - 05:12 AM
Yes, of course!


--[[ just me making an equation up
(x+3)(x^2+3x+1)
x^3+6x^2+9x+3
]]--

--#notice this is in the format ax^b, where tbl[ b ] = a

local input_tbl = {
  [0]=3,
  9,
  6,
  1,
}

local input_num = -3 --#negative three is a real solution to the problem; I will always pass it real solutions.

--#some function

local result = {
  [0]=1,
  3,
  1,
}

If that isn't clear enough, please let me know.
Edited on 23 October 2015 - 03:16 AM
Xtansia #4
Posted 23 October 2015 - 10:14 AM

--[[ just me making an equation up
(x+3)(x^2+3x+1)
x^3+6x^2+9x+3
]]--

Wanted to point out your math is wrong, it should be 10x not 9x.

And here's it implemented in lua: https://gist.github.com/Xtansia/e4e30318b890ea1dfdfe
synthetic_division returns the result polynomial table and the remainder.
Also added a function for converting the polynomial coefficient table into a string of the polynomial.
KingofGamesYami #5
Posted 23 October 2015 - 01:59 PM
Thanks! The only problem I've discovered is the polynomial_tostring doesn't like it if something is missing.

For example, this table was generated by your polynomial division:


{
  [3] = 1,
  [2] = 6, 
  [0] = -1,
}
Edited on 23 October 2015 - 11:59 AM
dan200 #6
Posted 23 October 2015 - 02:13 PM
This seems suspiciously like you're trying to get help with school homework!
Xtansia #7
Posted 23 October 2015 - 02:18 PM
Thanks! The only problem I've discovered is the polynomial_tostring doesn't like it if something is missing.

Oh, right fixed the gist.
Line 25 just needed 'or 0' added to the end, like so:

local coeff = polynomial[i] or 0
Creator #8
Posted 23 October 2015 - 02:29 PM
This seems suspiciously like you're trying to get help with school homework!

Is it forbidden? As long as we can find a cheap way to connect it to programming.
Edited on 23 October 2015 - 12:29 PM
KingofGamesYami #9
Posted 23 October 2015 - 05:28 PM
This seems suspiciously like you're trying to get help with school homework!

It's more of a project furthering my own understanding of what I've been taught… I learned synthetic division last year in Honors Algebra II, and I'm now applying it to polynomials in Honors Pre-Calculus.

In case anyone is wondering, my current progress can be found here -> http://pastebin.com/zxtG1xBK (this isn't done, I'm going to add imaginary solutions next)

Edit: I've now added imaginary solutions! Well, mostly…

Edit #2: Why does negative zero exist in lua? Ewwe. Fixed it.
Edited on 23 October 2015 - 04:08 PM
TYKUHN2 #10
Posted 23 October 2015 - 10:19 PM
I recently did this and because I am becoming increasingly ahead of my Honors class (the highest they offer here as well) and she knows I program my teacher is starting to challenge me to program a TI-83 to do what we do in class.

Also -0 is a thing I noticed. Also Java sometimes likes to pretend nil == 0 which screws things up…
Edited on 24 October 2015 - 01:54 AM
Engineer #11
Posted 24 October 2015 - 12:34 AM
I recently did this and because I am becoming increasingly ahead of my Honors class (the highest they offer here as well) and she knows I program my teacher is starting to challenge me to program a TI-83 to do what we do in class.

Also -0 is a thing I noticed. Also Java sometimes like to pretend nil == 0 which screws things up…
Add an extra check to work around that:

x = 0
if x and x == 0 then
   Blabla
end
My lua is rusty since I was tempting to put a colon for the then, python is my latest interest :P/>
Bomb Bloke #12
Posted 24 October 2015 - 01:49 AM
My Java is rusty, but don't certain number types initialise as 0, not nil? Certainly int etc arrays do.

Back in school they had us graphing quadratics; plotting out points for random values of x and guessing what the line between them looked like didn't sit well with me, so instead I wrote a program to draw the graphs properly and printed the output (which didn't sit well with the teacher).
TYKUHN2 #13
Posted 24 October 2015 - 03:56 AM
Pneumaticraft has(had) a drone method that grabbed the fuel, and 0 could be 0, -0, nil, and some insanely small number (both positive and negative) which forced me to use some very interesting system checks.