Divide a region by a predefined raster - grid

I have a big grid (indicated on the image in grey) that is divided in several blocks (each with a maximum width of 3 units). Now I would like to divide a region (indicated on the grid in red) by the corresponding blocks.
To give an example, I would like the 'block regions' (represented {X,Y,Width,Height}):
A: {2,3,2,1}
B: {4,3,3,1}
C: {7,3,1,1}
D: {2,4,2,3}
...
The only information that I have is the size of the blocks in the grid (in this case 3) and the dimensions of the region:
{2,3,6,5} (= {X,Y, width, height})
Does anybody know how to do this in an efficient way? I thought about the use of mround to calculate the first boundaries of the blocks, but that lead to a dead end. Thanks in advance!

Math looks a bit weird because you start from 1,1 but here is the gist of it,
Find the boundaries for X/Y-axises separately (numbers divisable by blocks size with in the grid)
All sizes will blocksize, blocksize except the left/right/top/bottom most boxes
Find the width/height for these special boxes
Places corresponding sizes to corresponding boundaries
zip two lists
-export([f/2]).
f({X,Y,Width, Height}, BlockSize) ->
% all sizes will be Blocksize, Blocksize except special cases
% left/right most will have Xfd/Xld
% top/bottom most will have Yfd/Yld
Xfd = BlockSize - ( ( X - 1) rem BlockSize) ,
Xld = (X + Width - 1) rem BlockSize,
Yfd = BlockSize - ( ( Y - 1) rem BlockSize) ,
Yld = (Y + Height - 1) rem BlockSize,
Xs = divs(X, Width, BlockSize, Xfd),
Ys = divs(Y, Height, BlockSize, Yfd),
%replace last values
{Lx, _} = lists:last(Xs),
Xss = lists:keyreplace(Lx, 1, Xs, {Lx, Xld}),
{Ly, _} = lists:last(Ys),
Yss = lists:keyreplace(Ly, 1, Ys, {Ly, Yld}),
R = merge(Xss, Yss),
io:format("~p~n", [R]).
merge(X, Y) ->
XY= lists:foldl(fun(Xx, Acc) ->
lists:foldl(fun(Yy, Acc1) ->
[{Xx, Yy} | Acc1]
end, Acc, Y)
end, [], X),
lists:map(fun({{Xx, W}, {Yy, H}}) ->
{Xx, Yy, W, H}
end , XY).
divs(X, W, BlockSize, Fd) ->
R = [{X, Fd} | [{Xs, BlockSize} || Xs<- lists:seq(X, X + W), (Xs - 1) rem BlockSize =:= 0]],
lists:sort(sets:to_list(sets:from_list(R))).
>[{7,7,1,1},
{7,4,1,3},
{7,3,1,1},
{4,7,3,1},
{4,4,3,3},
{4,3,3,1},
{2,7,2,1},
{2,4,2,3},
{2,3,2,1}]

For the fun I wrote this one.
-module(cluster).
-compile([export_all]).
-record(point, {x,y}).
-record(area, {x,y,w,h}).
area2points(A) ->
[#point{x=X,y=Y} ||
X <- lists:seq(A#area.x, A#area.x+ A#area.w-1),
Y <- lists:seq(A#area.y, A#area.y+ A#area.h-1)].
points2area([]) -> empty;
points2area(L) ->
lists:foldl(fun(P,Acc) -> fromcorner(P,Acc) end, area(),L).
area() -> #area{}.
area(X,Y,W,H) -> #area{x=X,y=Y,w=W,h=H}.
point() -> #point{}.
point(X,Y) -> #point{x=X,y=Y}.
fromcorner(#point{x=X,y=Y},#area{x=undefined,y=undefined}) ->
#area{x=X,y=Y,w=1,h=1};
fromcorner(#point{x=X,y=Y},#area{x=Xa,y=Ya,w=W,h=H}) ->
{Xmin,Wn} = case X < Xa of
true -> {X,Xa + W - X};
false -> {Xa,max(W, X+1-Xa)}
end,
{Ymin,Hn} = case Y < Ya of
true -> {Y,Ya + H - Y};
false -> {Ya,max(H, Y+1-Ya)}
end,
#area{x=Xmin,y=Ymin,w=Wn,h=Hn}.
split(A,L) ->
LA = area2points(A),
[points2area(lists:filter(fun(P) -> lists:member(P,LA) end ,area2points(X))) || X <- L].
test() ->
Z1 = area(1,1,3,3),
Z2 = area(4,1,3,3),
Z3 = area(7,1,3,3),
Z4 = area(1,4,3,3),
Z5 = area(4,4,3,3),
Z6 = area(7,4,3,3),
Z7 = area(1,7,3,3),
Z8 = area(4,7,3,3),
Z9 = area(7,7,3,3),
World = [Z1,Z2,Z3,Z4,Z5,Z6,Z7,Z8,Z9],
A = area(2,3,6,5),
split(A,World).
76> make:all().
Recompile: ../src/cluster
up_to_date
77> l(cluster).
{module,cluster}
78> cluster:test().
[{area,2,3,2,1},
{area,4,3,3,1},
{area,7,3,1,1},
{area,2,4,2,3},
{area,4,4,3,3},
{area,7,4,1,3},
{area,2,7,2,1},
{area,4,7,3,1},
{area,7,7,1,1}]
79>

Related

Possible error in Mathematica's NMinimize function

Can anybody verify that the following is a real Mathematica problem and I am not fooling myself.
The problem is that NMinimize in the following code does not give any error message nor any results.
I am running Windows 10 Build 1809 and Mathematica 12.0
The code runs as expected if n<180. But if n>=180 NMinimize does not give any output, no error, no results. Please note that I have more than enough memory (32GB), so it is not a memory problem.
n = 180;
a = -1; b = 1; fa = 1;
xs = Range[a, b, (b - a)/n];
ys = Subscript[y, #] & /# xs;
y1 = Differences[ys]; y1 = (Most[y1] + Rest[y1])/2;
y2 = Differences[ys, 2];
ys = ys[[2 ;; -2]];
eq = ((y1^2 + y1^4 - ys y2)^2 - 1 + y1^2).((y1^2 + y1^4 - ys y2)^2 -
1 + y1^2) /. {Subscript[y, a] -> fa, Subscript[y, b] -> fa};
NMinimize[eq, ys]

How to calculate final size after multiple convolutions with same padding?

Given I have the following:
s: stride
k: kernel size
i: input size
n: number of times a convolution layer was performed
With the convolution layer having the following parameters:
input = [b, i, i, c] (with batch size b and channel size c)
padding = 'SAME'
stride = s
kernel_size = k
Is there a mathematical way to calculate the final output size?
I can do the following to programmatically calculate the final output size:
final_size = i
for _ in range(n):
final_size = np.ceil(final_size / s)
Yes. ceil(ceil(x / n) / m) = ceil(x / (x * m)) (at least for integer n and m), so it should simply be final_size = np.ceil(i / (s ** n)).

Find "short enough" paths in a given graph

I need to design an algorithm to find a path in a public transportation system. In theory only the best (lowest cost) path is required, but in reality it's different. When traveling in a public transportation system, it's difficult to define the cost, it cannot be simplified to traveling time, waiting time, transfer time, bus/subway fees etc, are all need to take into consideration.
Firstly I need to simplify the problem, design a cost function which is a combination of all those "time" and "fee", then use a graph algorithm to find a few paths (3~5 paths). Finally present all these paths to the end users and let them make the decision.
The reason I need to present more than one path is that, for different users/situations these "time" and "fee" are different, thus presenting a few paths is better than just giving out the "best" path.
Algorithms like A* are good for finding the shortest path, but how can I find those "short enough" paths in a graph ? or how can I find the shortest N paths ?
BTW, I don't even need to find the shortest path, because in practice the end users never know the shortest path (unless the shortest path is obvious), they will be happy if the results are close to the shortest path.
A* star's "cost" is more versatile than you are thinking. A* is typically explained with nodes who's cost is simply a distance. We can, however, beef this up a little.
I'm not seeing a language you prefer, maybe Graph? Oh well, here's some c++:
namespace Astar
{
struct CostEvaluation
{
int distance_cost;
int transfer_cost;
// others
int costToTraverseNodes( const Node& first, const Node& second ) const
{
int distance = // apply distance_cost to distance between nodes
int transfer = // apply transfer_cost if there is a transfer between nodes
return distance + transfer;
}
}
}
Now, the actual implementation of your A* will take a CostEvaluation object to determine the cost on the route. If transfers don't matter, set the transfer_cost to zero.
As far as a "good enough" route: I'm sure other people would be able to help you better, but I feel like you might run into a situation where the program says something like "oh, you want to be there in an hour, but the best route only takes twenty minutes? Here, go around in circles for forty minutes, that's good enough".
As I hinted in my comments, it is possible to create a modified A* version which reports multiple routes. I just drove my implementation to the point where it is obviously confirming this statement.
The code below starts with a "classic" A* implementation, which I keep around so one can study the differences between "classic" and "modified".
The basic idea for the modified version is to start the search both forwards and backwards in parallel. This often also yields more robust results, given that the "greediness" of A* is largely driven by its heuristic function (h(x)). It is possible to construct cases, where the greediness opts for fast progress at the start of the route, while that route towards the end "slows down" drastically. Starting from both sides (source, target), this effect can be mitigated to bits. (If one calculates to the end, it should always be optimal routes, if not necessarily the same route. If one were to calculate to the "classic" ending condition in both directions a picture as the one below could result, showing that the both directions yield 2 different paths.
Now, the "explored lists" of both directions can be used to find out when while searching e.g. "forwards", the next node is already explored by the "backwards" search - or vice versa. Obviously, those "connection points" between the two searches yield a route, which is not necessarily optimal but - a valid route.
My implementation traces those intermediate routes and I did not bother to collect them. The traces show the id of the node where both exploration lists "meet" and the resulting 2 parts of the route (source -> meeting point, meeting point -> destination).
Now, using those intermediate lists along with some post processing, e.g. by means of evaluating the routes according to single dimensions of the heuristics function (e.g. comfort, speed, ...), it should be possible to find a good enough selection of routes, associated with different trade-offs in those dimensions.
The full F# script is around 340 lines - a bit too long for this site, so I will omit some nonessential parts (such as my rendering function, creating those bitmaps, etc.
module AStar =
module Internals =
let makeRoute (explo : Map<int,(int * float)>) at tgt =
let rec loop at acc =
let dst,c = explo.[at]
match at,dst with
| (_,b) when b = tgt -> (at,c) :: acc
| (_,b) -> loop b ((at,c) :: acc)
[(tgt,0.0)] # loop at []
let makeRouteBackward (exploBW : Map<int, (int * float)>) at tgt =
let rec loop at acc =
let src,c = exploBW.[at]
match at,src with
| (_,b) when b = tgt -> acc # [(at,c)]
| (_,b) -> loop b (acc # [at,c])
let r = loop at [] # [(tgt,0.0)]
let rev = List.rev r
List.zip r rev |> List.map (fun ((id1,c1),(id2,c2)) -> id1,c2)
let classic neighbors h cost start goal =
let prioSelect (lopen : (int * float) list) =
let sorted = List.sortBy (fun (id,p) -> p) lopen //|> List.rev
(fst (List.head sorted), List.tail sorted)
let rec search (lopen : (int * float) list) (routes : Map<int,int * float>) =
let rec searchNeighbors cur nl o (r : Map<int,(int * float)>) =
match nl with
| [] -> o,r
| next::others ->
let newCost = (snd (r.[cur])) + cost cur next
if (not (Map.containsKey next r)) || (newCost < snd r.[next])
then
let r1 = r |> Map.remove next |> Map.add next (cur,newCost)
let prio = newCost + h next goal
//printfn "current = %d -- next = %d -- newCost = %f -- prio = %f -- h = %f" cur next newCost prio (h next goal)
let o1 = (next,prio) :: o
searchNeighbors cur others o1 r1
else
searchNeighbors cur others o r
match lopen with
| [] -> []
| _::_ ->
let current,rest = prioSelect lopen
if current = goal then Internals.makeRoute routes current start
else
let lopen1,routes1 = searchNeighbors current (neighbors current) rest routes
search lopen1 routes1
search [start,0.] (Map.ofList [start,(start,0.0)])
let twoWay sources targets hforward hbackward costforward costbackward (start : int) (goal : int) (n : int) rr =
let prioSelect (lopen : (int * float) list) =
let sorted = List.sortBy (fun (id,p) -> p) lopen //|> List.rev
(fst (List.head sorted), List.tail sorted)
let searchforward lopen exploredF exploredB nfound acc =
let rec searchNeighbors cur nl o (r : Map<int,(int * float)>) =
match nl with
| [] -> o,r
| next::others ->
//printfn "fwd: current = %d -- next = %d -- nl = %A -- r = %A" cur next nl r
let newCost = (snd (r.[cur])) + costforward cur next
if (not (Map.containsKey next r)) || (newCost < snd r.[next])
then
let r1 = r |> Map.remove next |> Map.add next (cur,newCost)
let prio = newCost + hforward next goal
let o1 = (next,prio) :: o
if Map.containsKey next exploredB then
rr (next, Internals.makeRoute r1 next start, Internals.makeRouteBackward exploredB next goal)
searchNeighbors cur others o1 r1
else
searchNeighbors cur others o r
match lopen with
| [] -> (lopen,exploredF,0,acc)
| _::_ ->
let current,rest = prioSelect lopen
if current = goal then
(rest,exploredF,nfound+1,acc # [Internals.makeRoute exploredF current start] )
else
let lopen1,explored1 = searchNeighbors current (targets current) rest exploredF
(lopen1, explored1, nfound, acc)
let searchbackward lopen exploredB exploredF nfound acc =
let rec searchNeighbors cur nl o (r : Map<int,(int * float)>) =
match nl with
| [] -> o,r
| next::others ->
//printfn "bwd: current = %d -- next = %d -- nl = %A -- r = %A" cur next nl r
let newCost = (snd (r.[cur])) + costbackward cur next
if (not (Map.containsKey next r)) || (newCost < snd r.[next])
then
let r1 = r |> Map.remove next |> Map.add next (cur,newCost)
let prio = newCost + hbackward next start
let o1 = (next,prio) :: o
searchNeighbors cur others o1 r1
else
searchNeighbors cur others o r
match lopen with
| [] -> (lopen,exploredB,0,acc)
| _::_ ->
let current,rest = prioSelect lopen
if current = start then
//(rest,explored,nfound+1,acc # [Internals.makeRoute explored current goal []])
(rest,exploredB,nfound+1,acc # [Internals.makeRouteBackward exploredB current goal] )
else
let lopen1,explored1 = searchNeighbors current (sources current) rest exploredB
(lopen1, explored1, nfound, acc)
let rec driver openF openB exploredF exploredB nfoundF nfoundB accF accB =
let openF1, exploredF1,nfoundF1,accF1 = searchforward openF exploredF exploredB nfoundF accF
let openB1, exploredB1,nfoundB1,accB1 = searchbackward openB exploredB exploredF nfoundB accB
match (nfoundF1+nfoundB1), List.isEmpty openF1, List.isEmpty openB1 with
| (s,false,false) when s < n ->
driver openF1 openB1 exploredF1 exploredB1 nfoundF1 nfoundB1 accF1 accB1
| _ ->
accF1 # accB1
driver [start,0.0] [goal,0.0] (Map.ofList [start,(start,0.0)]) (Map.ofList [goal,(goal,0.0)]) 0 0 [] []
// Location : x,y coordinate or lat/long - whatever.
// Edges: (id,cost) list
type Node = { Id : int; Location : int * int; Edges : (int * float) list; EdgesBackward : (int * float) list}
type Graph = Map<int,Node>
let addNode node graph =
Map.add (node.Id) node graph
let newNode idgen x y =
{ Id = idgen(); Location = (x,y); Edges = []; EdgesBackward = [] }
let addEdge id cost node =
{ node with Node.Edges = node.Edges # [(id,cost)]; }
let addEdgeBackward id cost node =
{ node with Node.EdgesBackward = node.EdgesBackward # [(id,cost)]; }
let idgen startvalue =
let next = ref startvalue
fun () ->
let id = !next
next := !next + 1
id
let appendNode node nodeList = nodeList # [node]
let sq x = x*x
let distance p1 p2 =
let x1,y1 = p1
let x2,y2 = p2
sqrt( float (sq (x2-x1) + sq (y2-y1)) )
let solve (g : Graph) s e =
let ns id =
g.[id].Edges |> List.map (fun (id,c) -> id)
let h at goal =
float (distance (g.[at].Location) (g.[goal].Location))
let c a b =
g.[a].Edges |> List.pick (fun (id,cost) -> if id = b then Some(cost) else None)
[AStar.classic ns h c s e] // give it the same return type as solveTwoWay to make stuff below easier and shorter
let solveTwoWay (g : Graph) s e n =
let edges id =
let nl = g.[id].Edges |> List.map (fun (id,c) -> id)
//printfn "2way edges id = %d list = %A" id nl
nl
let edgesBackward id =
let nl = g.[id].EdgesBackward |> List.map (fun (id,c) -> id)
//printfn "2way backwards edges id = %d list = %A" id nl
nl
let hforward at goal =
float (distance (g.[at].Location) (g.[goal].Location))
let hbackward at start =
float (distance (g.[at].Location) (g.[start].Location))
let costF a b =
g.[a].Edges |> List.pick (fun (id,cost) -> if id = b then Some(cost) else None)
let costB a b =
g.[a].EdgesBackward |> List.pick (fun (id,cost) -> if id = b then Some(cost) else None)
let debugView arg =
let id,r1,r2 = arg
printfn "meeting at %d: r1 = %A r2 = %A" id r1 r2
AStar.twoWay edgesBackward edges hforward hbackward costF costB s e n debugView
let solveProblem problem =
let g, start, goal = problem
g,start,goal,solve g start goal
let solveProblemTwoWay problem n =
let g, start, goal = problem
g,start,goal,solveTwoWay g start goal n
let save name solution =
let graph, start, goal, routes = solution
use writer = System.IO.File.CreateText("""E:\temp\""" + name + """.txt""")
fprintf writer "------------------------------------\n start = %d ----> goal = %d: %d routes found.\n" start goal (List.length routes)
fprintf writer "Graph:\n"
graph |> Map.iter
(fun id node ->
fprintf writer "Node: %A\n" node
)
routes |> List.iteri
(fun index route ->
fprintf writer "Route %d: %A\n" index route
)
// An example problem I used to play with:
// The graph is such, that the nodes are connected to the right and
// downwards and diagonally downwards only.
// The cost is either 1.0 or sqrt(2), for the horizontal or vertical and
// the diagonal connection, respectively.
let problem2 () =
let newNodeAN = newNode (idgen 0)
let cond c x n =
if c then n |> x else n
let accessCost p =
match p with
| (4,4) | (4,5) | (5,4) | (5,5) -> 10.0
| _ -> 1.0
let right (n : Node) : Node =
let t = 1 + fst n.Location, snd n.Location
let c = accessCost t
n
|> cond (fst n.Location < 9) (fun n -> addEdge (n.Id + 1) c n)
|> cond (fst n.Location > 0) (fun n -> addEdgeBackward (n.Id - 1) c n)
let down n =
let t = fst n.Location, 1 + snd n.Location
let c = accessCost t
n
|> cond (snd n.Location < 9) (fun n -> addEdge (n.Id + 10) c n)
|> cond (snd n.Location > 0) (fun n -> addEdgeBackward (n.Id - 10) c n)
let diagdown n =
let t = 1 + fst n.Location, 1 + snd n.Location
let c = (sqrt(2.0)) * accessCost t
n
|> cond (fst n.Location < 9 && snd n.Location < 9) (fun n -> addEdge (n.Id + 11) c n)
|> cond (fst n.Location > 0 && snd n.Location > 0) (fun n -> addEdgeBackward (n.Id - 11) c n)
[
for y = 0 to 9 do
for x = 0 to 9 do
yield newNodeAN x y
]
|> List.map
(fun n ->
n
|> right
|> down
|> diagdown
)
|> List.map (fun n -> (n.Id,n))
|> Map.ofList
, 0, 99
// Last not least, the code can be executed like this:
// And since both implementations yield the same data structures,
// they can be used interchangeably and compared to each other.
solveProblemTwoWay (problem2() 5) |> save "problem2_solution"
The output printed during runtime which shows the "intermediate routes", then looks like this:
...
meeting at 48:
r1 = [(0, 0.0); (11, 1.414213562); (12, 2.414213562); (23, 3.828427125);
(34, 5.242640687); (35, 6.242640687); (46, 7.656854249); (47, 8.656854249);
(48, 9.656854249)]
r2 = [(48, 0.0); (58, 1.414213562); (68, 2.414213562); (78, 3.414213562);
(88, 4.414213562); (99, 5.414213562)]
meeting at 84:
r1 = [(0, 0.0); (11, 1.414213562); (21, 2.414213562); (32, 3.828427125);
(43, 5.242640687); (53, 6.242640687); (64, 7.656854249); (74, 8.656854249);
(84, 9.656854249)]
r2 = [(84, 0.0); (85, 1.414213562); (86, 2.414213562); (87, 3.414213562);
(88, 4.414213562); (99, 5.414213562)]
meeting at 95:
r1 = [(0, 0.0); (11, 1.414213562); (21, 2.414213562); (32, 3.828427125);
(43, 5.242640687); (53, 6.242640687); (64, 7.656854249); (75, 9.071067812);
(85, 10.07106781); (95, 11.07106781)]
r2 = [(95, 0.0); (96, 1.0); (97, 2.0); (98, 3.0); (99, 4.0)]
...

evaluating many functions at a single point using map

I was able to make a nice picture with Elm's share-elm.com any tips for code optimization would be appreciated but I am focusing on the last two lines:
xflip : (number, number) -> (number, number)
xflip pt = ( -1*(fst pt), snd pt)
rot : (number, number) -> (number, number)
rot pt = ( -1*(snd pt), fst pt)
mul : number -> (number, number) -> (number, number)
mul a b = (a*(fst b), a*(snd b))
add : (number, number) -> (number, number) -> (number, number)
add a b = ((fst a)+(fst b), (snd a)+(snd b))
-- implementations of the symmetries of hilbert space curve
t1 : (number, number) -> (number, number)
t1 b = (add (mul 0.5 (-100,-100)) ((mul 0.5) (rot (rot(rot (xflip b))) )))
t2 : (number, number) -> (number, number)
t2 b = (add (mul 0.5 (-100,100)) ((mul 0.5) (b)))
t3 : (number, number) -> (number, number)
t3 b = (add (mul 0.5 (100,100)) ((mul 0.5) ( b)))
t4 : (number, number) -> (number, number)
t4 b = (add (mul 0.5 (100,-100)) ((mul 0.5) (rot (xflip b) )))
--
t : [(number, number)] -> [(number, number)]
t z = (map t1 z) ++ (map t2 z) ++ (map t3 z) ++ (map t4 z)
I don't know if this is the best say to define vector addition or 2D transformations, but I needed to do it somehow. Often done with vector graphics on the graphics themselves, I am working with list of points before they become Path types.
Was this the best way to iterate the rotation function rot ? I needed to rotate 90 degrees left and then right. So I rotated left 3 times:
rot (rot(rot (xflip b)))
Onto the main question, could my last two lines be streamlined:
t : [(number, number)] -> [(number, number)]
t z = (map t1 z) ++ (map t2 z) ++ (map t3 z) ++ (map t4 z)
The list of numbers are will become my Path objects and t1 through t4 are functions. I thought maybe I could iterate over these functions with map. It works in the cases I tried on Github gist: https://gist.github.com/MonsieurCactus/ef285584f1588289b477 Here's what I tried:
t : [(number, number)] -> [(number, number)]
t z = map ( \f -> (map f z)) [t1, t2, t3 ,t4]
The Elm compiler returned the error message:
[1 of 1] Compiling Main ( Main.elm )
Type error on line 49, column 7 to 46:
map (\f -> map f z) [t1,t2,t3,t4]
Expected Type: (Float)
Actual Type: _List
Type error on line 49, column 7 to 46:
map (\f -> map f z) [t1,t2,t3,t4]
Expected Type: Float
Actual Type: (Float, Float)
Maybe I should have tried writing a function [Path] -> [Path] but then I have to get the list of points and change them anyway.
Streamlining the last two lines
Your attempt at shortening the definition of t is in the right direction. But because you map over the list of functions ([t1,t2,t3,t4]), and inside the mapping function you map over the list of points z, you end up with a list of lists of points ([[(number,number)]] instead of [(number, number)]).
So you still need to concat that list of lists. You can also use concatMap instead of a loose concat and map:
t : [(number, number)] -> [(number, number)]
t z = concatMap ( \f -> (map f z)) [t1, t2, t3 ,t4]
Iterating rot
If you don't mind using Float everywhere instead of number, you can change your rot function to take a rotation to perform. Using some basic functions, you could write something like:
rot' : Float -> (Float, Float) -> (Float, Float)
rot' angle point =
let (r,th) = toPolar point
th' = th + angle
in fromPolar (r,th')
rot = rot' (degrees 90)

idl/gdl ERROR: function not found or scalar subscript out of range

i try to solve this problem with my code. When i compile i have the follow error message:
% POINCARE: Ambiguous: POINCARE: Function not found: XT or: POINCARE: Scalar subscript out of range [>].e
% Execution halted at: POINCARE 38 poincare.pro
% $MAIN$
It's very simple:
1) i OPEN THE FILE AND COUNT THE NUMBER OF ROWS AND COLUMNS,
2) save the fle in a matrix of ROWSxCOLUMNS,
3) take the rows that i want and save them as vectors,
Now i want to modify the columns as follow:
A) translate each element of first and second column (x and y) by a costant factor (xc, yc ....)
B) apply some manipulation of each new element of this two new columns (xn ,yn ...)
C) if the value pyn is greater than 0. then save the rows with the four value of xn ,pxn.
Here the code:
pro poincare
file = "orbitm.txt"
rows =File_Lines(file) ; per le righe
openr,lun,file,/Get_lun ; per le colonne
line=""
readf,lun,line
cols = n_elements(StrSplit(line, /RegEx, /extract))
openr,1,"orbitm.txt"
data = dblarr(cols,rows)
readf,1,data
close,1
x = data(0,*) ; colonne e righe
y = data(1,*)
px = data(2,*)
py = data(3,*)
mu =0.001
xc = 0.5-mu
yc = 0.5*sqrt(3.)
openw,3,"section.txt"
for i=1, rows-2 do begin
xt = x(i)-xc
yt = y(i)-yc
pxt = px(i)-yc
pyt = py(i)+xc
tau = y(i)/(y(i)-y(i+1))
xn = xt(i) + (xt(i+1)-xt(i))*tau
yn = yt(i) + (yt(i+1)-yt(i))*tau
pxn = pxt(i) + (pxt(i+1)-pxt(i))*tau
pyn = pyt(i) + (pyt(i+1)-pyt(i))*tau
if (pyt(i) GT 0.) then begin
printf,3, xt(i), pxt(i)
endif
endfor
close,3
end
I attach also the first rows of my input orbitm.txt:
0.73634 0.66957 0.66062 -0.73503
0.86769 0.54316 0.51413 -0.82823
0.82106 0.66553 0.60353 -0.74436
0.59526 0.88356 0.79569 -0.52813
0.28631 1.0193 0.92796 -0.24641
-0.29229E-02 1.0458 0.96862 0.21874E-01
-0.21583 1.0090 0.95142 0.22650
-0.33994 0.96091 0.92099 0.35144
-0.38121 0.93413 0.90831 0.39745
-0.34462 0.93959 0.92534 0.36561
-0.22744 0.96833 0.96431 0.25054
-0.24560E-01 0.99010 0.99480 0.45173E-01
0.25324 0.95506 0.96459 -0.24000
0.55393 0.81943 0.82584 -0.54830
0.78756 0.61644 0.61023 -0.77367
0.88695 0.53076 0.50350 -0.82814
I can see a few issues that are immediately obvious. The first is that you define the variables XT, YT, PXT, and PYT inside your FOR loop as scalars. Shortly after, you try to index them as if they are arrays with multiple elements. Either your definition for these variables needs to change, or you need to change your definition of XN, YN, PXN, and PYN. Otherwise, this will not work as written. I have attached a modified version of your code with some suggestions and comments included.
pro poincare
file = "orbitm.txt"
rows =File_Lines(file) ; per le righe
openr,lun,file,/Get_lun ; per le colonne
line=""
readf,lun,line
cols = n_elements(StrSplit(line, /RegEx, /extract))
free_lun,lun ;; need to close this LUN
;; define data array
data = dblarr(cols,rows)
;;openr,1,"orbitm.txt"
;;readf,1,data
;; data = dblarr(cols,rows)
;;close,1
openr,lun,"orbitm.txt",/get_lun
readf,lun,data
free_lun,lun ;; close this LUN
;;x = data(0,*) ; colonne e righe
;;y = data(1,*)
;;px = data(2,*)
;;py = data(3,*)
x = data[0,*] ;; use []'s instead of ()'s in IDL
y = data[1,*]
px = data[2,*]
py = data[3,*]
mu = 0.001
xc = 0.5 - mu ;; currently a scalar
yc = 0.5*sqrt(3.) ;; currently a scalar
;; Perhaps you want to define XT, YT, PXT, and PYT as:
;; xt = x - xc[0]
;; yt = y - yc[0]
;; pxt = px - yc[0]
;; pyt = py + xc[0]
;; Then you could index these inside the FOR loop and
;; remove their definitions therein.
;;openw,3,"section.txt"
openw,lun,"section.txt",/get_lun
for i=1L, rows[0] - 2L do begin
xt = x[i] - xc ;; currently a scalar
yt = y[i] - yc ;; currently a scalar
pxt = px[i] - yc ;; currently a scalar
pyt = py[i] + xc ;; currently a scalar
tau = y[i]/(y[i] - y[i+1]) ;; currently a scalar
;; In the following you are trying to index XT, YT, PXT, and PYT but
;; each are scalars, not arrays!
xn = xt[i] + (xt[i+1] - xt[i])*tau
yn = yt[i] + (yt[i+1] - yt[i])*tau
pxn = pxt[i] + (pxt[i+1] - pxt[i])*tau
pyn = pyt[i] + (pyt[i+1] - pyt[i])*tau
if (pyt[i] GT 0.) then begin
printf,lun, xt[i], pxt[i]
endif
endfor
free_lun,lun ;; close this LUN
;;close,3
;; Return
return
end
General IDL Notes: You should use []'s instead of ()'s to index arrays to avoid confusion with functions. It is generally better to let IDL define a logical unit number (LUN) and then free the LUN than use CLOSE.

Resources