TransWikia.com

How apply a function to multiple nested elements without a loop

Mathematica Asked on August 16, 2021

I’m trying to speed up solving the intersection of groups of lines with a surface. At the moment I have a function that loops through each line in a group (i), then goes to the next group (j).
The lines are defined as a bunch of positions (x,y,z) and directions (mx,my,mz) These are in separate lists called pos and dir, with the same nested structure:

pos = {{{x11,y11,z11},{x12,y12,z12},{z13,y13,z13}...{x1n,y1n,z1n}},{{x21,y21,z21},{x22,y22,z22},{z23,y23,z23},...{x2n,y2n,z2n}}... ...{xmn,ymn,zmn}}}
dir = {{{dx11,dy11,dz11},{dx12,dy12,dz12}, {dz13,dy13,dz13} ... {dx1n,dy1n,dz1n}}, {{dx21,dy21, dz21}, {dx22, dy22,dz22},{dz23,dy23,dz23}, ... {dx2n,dy2n,dz2n}} ... ... {dxmn,dymn,dzmn}}}


f[x0_, y0_, z0_, mx_, my_, mz_] =  NSolve[lm[x0 + t mx, y0 + t my] == z0 + t mz, t];

lm happens to be a polynomial in x and y but I presume the syntax for efficiently mapping this will not depend on that:

0 == 0.217238 + 0.00974771 x + 2.00034 x^2 - 22.958 x^3 - 135260. x^4 + 0.0337732 y - 0.479033 x y - 96.3136 x^2 y + 1303.65 x^3 y - 33.0002 y^2 + 4.03579 x y^2 - 43360.3 x^2 y^2 + 32.6967 y^3 - 288.111 x y^3 - 39860.3 y^4

At the moment, I am doing this:

result = ParallelTable[ f[pos[[i, j, 1]],pos[[i, j, 2]],pos[[i, j, 3]],dir[[i, j, 1]],dir[[i, j, 2]],dir[[i, j, 3]]], {i, Length@pos}, {j,Length[pos[[i]]]}]

How do I use Map to do this for the nested list rather than using loops, I assume Map is the optimum way to do this, with maybe a reformatting the pos and dir data first?

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP