|
Hi, I am working on an iOS app that needs to do a lot of op data lookup in tables with data, most of the time this data has to be interpolated as well. Here is an example: Then when calling a function:
I have around 30 of those tables. What is the best place to store this data, so the function can look up the appropriate entry and do the 3D interpolation? I was thinking of a nested array, but perhaps there is a better option. Cheers, Bastiaan |
|
Hi Bastiaan! I am facing a similar issue. Did you come up with a solution yet? Toine |
|
Hi, I would create static dictionaries and store this that way. For example with the second table, your first key would be the It is going to be pain to write, you could possibly check out some tools to generate Swift syntax (https://nshipster.com/swift-gyb/), but it will be quite nice to use, fast and you will see all the values in your source code. |
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts! Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
Thanks for the suggestion, but the problem is that these values have to be (linearly) interpolated. I tried to train a ML model, but did not achieve the accuracy that I desired. I think I will stick with dictionaries and interpolate myself! |
|
You are right, dictionaries are probably not the best choice when you need to interpolate across both axis. |
|
hi, i would suggest thinking of any of these tables as triples of 3D points first, the arithmetic for your example, using x = 28 and y = 2300: locate the four points in the table that surround it and compute appropriate weights (from 0 to 1) depending on how close the x = 28 and y = 2300 are to the x- and y-coordinates of the points, respectively, like this
the sum of the four weighted z-contributions is 3.78625, or the 3.79 you want. the xWeight contribution is just to turn this into code ... this works in my playground:
that's one sample computation only; to turn this into a function to support interpolation throughout the table
this second part might sound hard, but i think it will be enough to just find just the four "closest" points that minimize the x and y differences (e.g., the table values x = 26 and x = 30 are the two closest to 28; the table values y = 2000 and y = 4000 are the two closest to y = 2300). you'll have to handle the boundary cases yourself (e.g., the x and y are already in the table, or the x and y lie outside the range of the table), but that sounds easy. hope that helps, DMG |
|
Hi, i have interpolated my data set. I would like to check which method will be best suited for my data. Reference data for cross validation is missing. Is any other method exit with which i can check which interpolation technique will be best suited for my data set? |
|
Would using Core Data, NSSortDescriptors and Predicate be useful here? Each table would have it own entity type, and have the atttributes xCoord, yCoord, itemValue. The use Fetch, NSSortDescriptors and NSPredicates to pull out the four corners of the interpolation region, as a set of 4 entities. Just a thought. |
|
First of all, my apologies for my late response, this part of the project got sidetracked; I picked it up again. Secondly, thank you for you're ideas. Finally, if there are still people who seek a similar solution, I approached it as delawaremathguy suggested. To show my solution, this is the table used in this example: First, I put all the data in a struct:
Then with an if the else I assign the correct table, e.g.:
Then the tedious job starts to find the range of the columns and rows.
A simple 2D interpolation is used to find the correct value:
For example:
And so on. Still, I hope there is a more straightforward solution. This operation is, for example in Numbers, or Excel so much easier. Cheers, Bastiaan |
|
hi Bastiaan, i am not sure why i have a fascination with this question ... although i do know some math (!) ... and then you threw a monkey wrench into your solution for a different table that requires (apparently) four input parameters (weight, temperature, wind corrected brakes (?), and pressure/altitude). that's a lot different than the simple 2D lookup in the original question. anyway, it got me to thinking, and i have something for your consideration: a general scheme for looking up values (Doubles) in a table based on a number of input parameters (doubles) that have a fixed range, and for which values are known at certain points of the input range (e.g., there are known values for weight = 40, 50, 60, 70, 80, and for pressure/altitude = 0, 5, 10). so i suggest a recursive computation, recursing on the number of inputs. that is, you tell me that you need to input temperature and pressure/altitude, and i will compare the temperature against reference temperature points that you have defined (2, 6, 10, 14 , ... 54) and known pressure/altitude points that you have defined (0, 2000, 4000, ... , 10000) and i will ratio out the known values one at a time:
and to make this work, i require you to store the known values for each combination of known reference inputs in a dictionary, keyed by the inputs. example:
another example:
so, let's define a protocol for how to do a table lookup of a value based on some number of inputs:
the so for your brake cooling schedule, you define this:
and when you want an interpolated value, you do this:
now,
will compute 65% of once the recursion goes 4 levels deep in this case, you'll be looking at a combination of weighted averages of weighted averages ... of weighted averages such as here's what that looks like:
i think this works, based on my limited testing ... but if i have missed something, i'm not very far off. recursion is a tricky thing and very mathematical ... sometimes the best recursive routines are the ones that don't seem to do very much. the only requirement to then implement any table lookup is to define a struct that adopts the wow ... just could not let this one go ... hope that helps, DMG |
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.