Carl Love

Carl Love

28100 Reputation

25 Badges

13 years, 106 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are replies submitted by Carl Love

@alpha041 Here's the worksheet with the working ApplyWeights command.


restart:

randomize(2): #Needed to get nontrivial minimal paths.

N:= 100:  #Number of vertices

E:= 2300: #Number if directed edges

CartProdSeq:= proc(L::seq(list))

local Seq,i,j;

option `Copyright (C) 2007, Joseph Riel. All rights reserved.`;

    eval({subs(Seq=seq, foldl(Seq

                              , [cat(i,1..nargs)]

                              , seq(cat(i,j)=L[j],j=nargs..1,-1)

                             ))});

end proc:


AllPossibleEdges:= [
     (CartProdSeq([$1..N], [$1..N]) minus
           {seq([k,k], k= 1..N)}
      )[]
]:

Edges:= combinat:-randcomb(AllPossibleEdges, E):

Weights1:= RandomTools:-Generate(list(float(method= uniform), E)):

WeightedEdges1:= {zip(`[]`, Edges, Weights1)[]}:

G1:= GraphTheory:-Graph(WeightedEdges1);

GRAPHLN(directed, weighted, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], Array(%id = 18446744074187374710), `GRAPHLN/table/1`, Matrix(%id = 18446744074216096014))

Weights2:= RandomTools:-Generate(list(float(method= uniform), E)):

WeightedEdges2:= {zip(`[]`, Edges, Weights2)[]}:

G2:= GraphTheory:-Graph(WeightedEdges2);

GRAPHLN(directed, weighted, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100], Array(%id = 18446744074187375070), `GRAPHLN/table/2`, Matrix(%id = 18446744074198802974))

(Path1,Cost1):= GraphTheory:-DijkstrasAlgorithm(G1, 1, N)[];

[1, 31, 52, 58, 54, 14, 100], .199037223686419

(Path2,Cost2):= GraphTheory:-DijkstrasAlgorithm(G2, 1, N)[];

[1, 49, 24, 5, 14, 100], .418737682605255

Path_to_Edges:= proc(P::list)
local k;
     {seq([P[k], P[k+1]], k= 1..nops(P)-1)}
end proc:


PPath1:= Path_to_Edges(Path1);

{[1, 31], [14, 100], [31, 52], [52, 58], [54, 14], [58, 54]}

PPath2:= Path_to_Edges(Path2);

{[1, 49], [5, 14], [14, 100], [24, 5], [49, 24]}

G3:= GraphTheory:-Graph(PPath1 union PPath2);

GRAPHLN(directed, unweighted, [1, 5, 14, 24, 31, 49, 52, 54, 58, 100], Array(%id = 18446744074187376150), `GRAPHLN/table/3`, 0)

WeightOfPath:= proc(G::GraphTheory:-Graph, P::list)
local
     e,
     W:= GraphTheory:-WeightMatrix(G, copy= false)
;
     add(W[e[]], e in Path_to_Edges(P))
end proc:


WeightOfPath(G1, Path1);

.199037223686419

WeightOfPath(G2, Path2);

.418737682605255

ApplyWeights:= proc(G1::GraphTheory:-Graph, G2::GraphTheory:-Graph)
#Apply the weights from G1 to G2.
uses GT= GraphTheory;
local W:= GT:-WeightMatrix(G1, copy= false);
     GT:-Graph(
          map(e-> [e, W[e[]]], GT:-Edges(G2, weights= false))
     )
end proc:          


G4:= ApplyWeights(G1, G3);

GRAPHLN(directed, weighted, [1, 5, 14, 24, 31, 49, 52, 54, 58, 100], Array(%id = 18446744074187376390), `GRAPHLN/table/4`, Matrix(%id = 18446744074198482110))

G5:= ApplyWeights(G2, G3);

GRAPHLN(directed, weighted, [1, 5, 14, 24, 31, 49, 52, 54, 58, 100], Array(%id = 18446744074187376630), `GRAPHLN/table/5`, Matrix(%id = 18446744074211497430))

 


Download Two_Paths.mw

@jentox The error message indicates that it is not recognizing the ExcelTools package. Try putting

with(ExcelTools);

on a line by itself and reporting to us the results.

Please attach your worksheet so that we can work with it. Use the green up-arrow, which is the last item on the second row of the toolbar in the MaplePrimes editor.

Forward compatibility has nothing to do with it. Maple is backward compatible, which is what you need. Backward compatible means that code written for older versions still runs on newer versions.

Rather than using cut-and-paste, you can use the read command to input your module. It's simply

read "filename";

@alpha041 Well, like I asked before, suppose that the two paths have a common edge, but that the weights are different in the two representations. How do you choose the weight for that edge?

@alpha041 I just answered this question in your other thread, "paths in graph". Your problem was that edges need to be represented by pairs. A path representation such as [1, 5, 9, 100] will not work. That needs to be converted to {[1,5], [5,9], [9,100]}.

inttrans[fourier](3*cos(2000*Pi*t), t, f);

Also, we need to know u__1[i] and u__2[i] or have recursive equations for them.

You might as well ask how to solve int(f(x)*g(x), x= 0..16).

Your first equation is cubic in x[i+1], and your second equation is quadratic in y[i+1]. That means that there are six possibilities for [x[1], y[1], z[1]], 36 possibilities for [x[2], y[2], z[2]], etc. We need to use a selection criterion for the roots, such as using the greatest real roots for each.

@alpha041 You'll need to upload your worksheet for me to diagnose it.

@alpha041 What do you want to do if the same edge appears on both paths, but with different weights?

Your error probably arises from mixing lists and sets. If you emulated my code, then Path1 and Path2 are lists, so you can't use union on them. Simply change the code to

GraphTheory:-Graph({Path1[], Path2[]});

Let me know if that doesn't work.

@Muhammad Ali 

I don't know if it can be done with Statistics:-SurfacePlot, which crudely forms the data into a 3 x 441 GRID. If your data is to be made into a GRID, it should clearly be a 21x21 GRID. See ?plot,structure for the definitions of GRID and MESH. Statistics:-SurfacePlot is not smart enough to detect that there is a third dimension of organization hidden in your 2d data.

@Muhammad Ali 

map and seq are built-in, but zip is library code. Check out showstat(zip). So, zip(f, K, M) either uses a seq or a for loop, depending on the types of K and M.

When dealing with one container instead of two (so that zip doesn't apply), I'd use map(f, L) if the container L already exists. I'd use a seq if the container does not exist. But the difference in timing is very subtle and small, and could be influenced by a variety of other factors.

@taro 

a &* b is the same as `&*`(a,b). The quotes are necessary in the prefix form. Actually, this works for almost all infix operators, not just the neutral ones: a + b is the same as `+`(a,b).

First 539 540 541 542 543 544 545 Last Page 541 of 709