TreeForm without overlap - graph

I sometimes run into the problem of labels in TreeForm being unreadable because of overlap. An example is below, can anyone see a way to get rid of overlap?
{{4, 5, 6}, {{{2, 4, 5, 6}, {{{1, 2, 4}, {}}, {{2, 3, 6}, {}}}}, {{4,
5, 6, 8}, {{{4, 7, 8}, {}}, {{6, 8, 9}, {}}}}}} // TreeForm
(source: yaroslavvb.com)
Belisarius' solution helps with overlap, but loses Tooltips, ie compare with
TreeForm[Hold[
GraphPlotHighlight[edges : {((_ -> _) | {_ -> _, _}) ...},
hl : {___} : {}, opts : OptionsPattern[]] :=
Module[{verts, coords, g, sub}, 5]]]
(source: yaroslavvb.com)
Answer update 11/12
I ended up using code below (belisarius' code with a minor fix)
myTreeForm[exp_] :=
Module[{tooltipText, i},
tooltipText =
Cases[Cases[MakeBoxes[TreeForm#exp, StandardForm],
TooltipBox[x__] -> x, 7, Heads -> True],
TagBox[x__, y__] -> DisplayForm[First#{x}], Heads -> True];
i = 0;
TreeForm[exp,
VertexRenderingFunction -> ({Tooltip[
Inset[Rasterize[Text[" " <> ToString##2 <> " "],
Background -> LightBlue], #1], tooltipText[[i++]]]} &)]];

I did this before, but never generalized the result.
rectOffset = {.25,.1};
fontSize = 10
TreeForm[list,
VertexRenderingFunction -> ({White, EdgeForm[Black],
Rectangle[#1 - rectOffset, #1 + rectOffset], Black,
Text[ Style[#2, fontSize], #1]} &)]
Edit With Tooltips
Using a "different approach"
Code is dirty, sorry no time to clean it up right now
rectOffset = {.33, .1};
fontSize = 9;
p = Cases[
Cases[MakeBoxes[TreeForm#list, StandardForm], TooltipBox[x__] -> x,
7, Heads -> True], TagBox[x__, y__] -> DisplayForm[First#{x}],
Heads -> True];
i = 0;
TreeForm[list,
VertexRenderingFunction -> ({White, EdgeForm[Black],
Rectangle[#1 - rectOffset, #1 + rectOffset], Black,
Tooltip[Text[Style[#2, fontSize], #1], p[[i++]]]} &)]
Output
Edit 2
I think this version is better:
Clear["Global`*"];
list = Hold[
GraphPlotHighlight[edges : {((_ -> _) | {_ -> _, _}) ...},
hl : {___} : {}, opts : OptionsPattern[]] :=
Module[{verts, coords, g, sub}, 5]];
myTreeForm[exp_] :=
Module[{ps, tooltipText, i},
ps[text_] := Rasterize[Text[Style[text]], "RasterSize"];
tooltipText =
Cases[Cases[MakeBoxes[TreeForm#list, StandardForm],
TooltipBox[x__] -> x, 7, Heads -> True],
TagBox[x__, y__] -> DisplayForm[First#{x}], Heads -> True];
i = 0;
TreeForm[list,
EdgeRenderingFunction -> ({Red, Line[#1]} &),
VertexRenderingFunction -> ({White, EdgeForm[Black], {}, Black,
Tooltip[
Inset[Rasterize[Text[" " <> ToString##2 <> " "],
Background -> LightBlue], #1], tooltipText[[i++]]]} &)]
];
list // myTreeForm
Output:
Edit 4 ... and last one
Cleaned up code, remove spurious functions and variables that were there just to complicate things:
myTreeForm[list_] := Module[{tooltipText, i},
tooltipText =
Cases[Cases[MakeBoxes[TreeForm#list, StandardForm],
TooltipBox[x__] -> x, 7, Heads -> True],
TagBox[x__, y__] -> DisplayForm[First#{x}], Heads -> True];
i = 0;
TreeForm[list,
VertexRenderingFunction ->
({Tooltip[Inset[Rasterize[Text[" " <> ToString##2 <> " "],
Background -> LightBlue], #1], tooltipText[[i++]]]} &)
]
];
HTH!

It looks as if the option VertexCoordinateRules may be your best hope.

Related

How to use reverse Scaling function with error bars in mathematica?

I have data which I would like to plot along with the corresponding error bars:
{{{54927.7, -1.91044}, ErrorBar[38.2664, 0.0538982]},
{{55320.9, -1.97673}, ErrorBar[45.3592, 0.101486]},
{{55671.4, -2.15716}, ErrorBar[41.2234, 0.0258249]},
{{56032.9, -2.15957}, ErrorBar[38.8805, 0.0191277]},
{{56410.6, -2.14289}, ErrorBar[41.5501, 0.0189911]},
{{56787.2, -2.19703}, ErrorBar[38.1972, 0.00632055]},
{{57137.5, -2.1839}, ErrorBar[35.6098, 0.0084108]},
{{57493.3, -2.19994}, ErrorBar[38.0298, 0.00651633]},
{{57859.5, -2.19687}, ErrorBar[40.9682, 0.00658857]}}
I can use the "ErrorListPlot" function in *Mathematica just fine, however if I would like to reverse the y axis scale with the function "ScalingFunctions->"Reverse" " the error bars do not get plotted along with the data.....any suggestions on how to fix this?
Similar to this question. Some messing around to get the ticks looking right.
Needs["ErrorBarPlots`"]
data = {
{{54927.7, -1.91044}, ErrorBar[38.2664, 0.0538982]},
{{55320.9, -1.97673}, ErrorBar[45.3592, 0.101486]},
{{55671.4, -2.15716}, ErrorBar[41.2234, 0.0258249]},
{{56032.9, -2.15957}, ErrorBar[38.8805, 0.0191277]},
{{56410.6, -2.14289}, ErrorBar[41.5501, 0.0189911]},
{{56787.2, -2.19703}, ErrorBar[38.1972, 0.00632055]},
{{57137.5, -2.18390}, ErrorBar[35.6098, 0.0084108]},
{{57493.3, -2.19994}, ErrorBar[38.0298, 0.00651633]},
{{57859.5, -2.19687}, ErrorBar[40.9682, 0.00658857]}};
data[[All, 1, 2]] = -data[[All, 1, 2]];
ep = ErrorListPlot[data];
newTicks = AbsoluteOptions[ep, Ticks][[1, 2, 2]] /.
{x1_, x2_, x3_, x4_} :> If[x1 == x2,
{x1, NumberForm[-x2, {3, 2}], {0.014, 0}, x4},
{x1, x2, {0.007, 0}, x4}];
newTicks = newTicks /. {
GrayLevel[0.] -> GrayLevel[0.5],
AbsoluteThickness[0.25] -> AbsoluteThickness[0.18],
AbsoluteThickness[0.125] -> AbsoluteThickness[0.08]};
ErrorListPlot[data, Ticks -> {Automatic, newTicks}]
Needs["ErrorBarPlots`"]
data = {
{{54927.7, -1.91044}, ErrorBar[38.2664, 0.0538982]},
{{55320.9, -1.97673}, ErrorBar[45.3592, 0.101486]},
{{55671.4, -2.15716}, ErrorBar[41.2234, 0.0258249]},
{{56032.9, -2.15957}, ErrorBar[38.8805, 0.0191277]},
{{56410.6, -2.14289}, ErrorBar[41.5501, 0.0189911]},
{{56787.2, -2.19703}, ErrorBar[38.1972, 0.00632055]},
{{57137.5, -2.18390}, ErrorBar[35.6098, 0.0084108]},
{{57493.3, -2.19994}, ErrorBar[38.0298, 0.00651633]},
{{57859.5, -2.19687}, ErrorBar[40.9682, 0.00658857]}};
(* Invert the error bars, visible in InputForm *)
ep = InputForm[ErrorListPlot[data]] /.
{Line[{{a_, b_}, {c_, d_}}] :> Line[{{a, -b}, {c, -d}}],
Line[{Offset[e_, {f_, g_}], Offset[h_, {i_, j_}]}] :>
Line[{Offset[e, {f, -g}], Offset[h, {i, -j}]}]};
(* Discard the InputForm wrapper *)
ep2 = First[ep];
Show[ErrorListPlot[data, ScalingFunctions -> "Reverse"],
Delete[ep2, Most#First#Position[ep2, Point]]]

Return the index for a list from a list in Erlang

I've been practicing using recursion to define the index in Erlang. Here I need to implement a function to return the index for a list from a list.
eg.
([2, 4, 4], [1, 1, 2, 4, 4, 3, 4 ]) ----> 2
([1, 3], [5, 2, 2, 3, 1, 3, 5]) ----> 4
([1], [3, 2, a, {1, 1}, 1] ----> 4
Here is my code:
-module(project).
-export([index/2]).
index([X|XS],[_]) -> index([X|XS],[_],1).
index(_,[],_) -> [];
index([X|XS],[X|_], ACC) -> ACC;
index([X|XS],[_|rest],ACC) ->index([X|XS],rest,ACC+1).
I modified and coded logically but it still can not being compiled. I hope someone who can help me with it. Thanks!
Just for fun, here is an implementation that is not written a very clean way, but illustrates the techniques I think you are looking for. Note there are two basic states: "checking" and "matching".
-module(sublistmatch).
-export([check/2]).
check(Segment, List) ->
SegLen = length(Segment),
ListLen = length(List),
Index = 1,
check(Segment, List, SegLen, ListLen, Index).
check(S, S, _, _, I) ->
{ok, I};
check(_, _, SL, LL, _) when SL >= LL ->
nomatch;
check(S = [H|Ss], [H|Ls], SL, LL, I) ->
case matches(Ss, Ls) of
true -> {ok, I};
false -> check(S, Ls, SL, LL - 1, I + 1)
end;
check(S, [_|L], SL, LL, I) ->
check(S, L, SL, LL - 1, I + 1).
matches([H|S], [H|L]) -> matches(S, L);
matches([], _) -> true;
matches(_, _) -> false.
Note that this depends on knowing the lengths of both the segment you are checking for, and the current length of the remaining list to check. Consider why this is necessary. Also consider how using the utility function matches/2 gives us a natural place to explore whether an option matches, and backtracks if it does not.
In real programs you would use the standard library functions such as lists:prefix/2, lists:suffix/2, or sets:is_subset/2, or maybe some key or member operation over a gb_tree, dict, map or array depending on the situation.
To Compile the code you have to change it to:
-module(project).
-export([index/2]).
%%index([X|XS],[_]) -> index([X|XS],[_],1).
index([X|XS],List) -> index([X|XS],List,1).
%% you shuld not pass '_' as parameter it's will be marked as unbound
index(_,[],_) -> [];
index([X|XS],[X|_], ACC) -> ACC;
%%index([X|XS],[_|rest],ACC) ->index([X|XS],rest,ACC+1).
index([X|XS],[_|Rest],ACC) ->index([X|XS],Rest,ACC+1).
%% rest is an atom, it's not the case you need to use here.
%%Variables should start with upper case letter.
This code will compiled but wrong results as some cases.

SML: Look and Say Function

I'm having trouble with writing the look and say function recursively. It's supposed to take a list of integers and evaluate to a list of integers that "reads as spoken." For instance,
look_and_say([1, 2, 2]) = "one one two twos" = [1, 1, 2, 2]
and
look_and_say([2, 2, 2]) = "three twos" = [3, 2]
I'm having some difficulty figuring out how to add elements to the list (and keep track of that list) throughout my recursive calls.
Here's an auxiliary function I've written that should be useful:
fun helper(current : int, count : int, remainingList : int list) : int list =
if (current = hd remainingList) then
helper(current, count + 1, tl remainingList)
else
(* add count number of current to list *)
helper(hd remainingList, 1, tl remainingList);
And here's a rough outline for my main function:
fun look_and_say(x::y : int list) : int list =
if x = nil then
(* print *)
else
helper(x, 1, y);
Thoughts?
You seem to have the right idea, although it doesn't look as if your helper will ever terminate. Here's a way of implementing it without a helper.
fun look_and_say [] = []
| look_and_say (x::xs) =
case look_and_say xs of
[] => [1,x]
| a::b::L => if x=b then (a+1)::b::L
else 1::x::a::b::L
And here's a way of implementing it with your helper.
fun helper(current, count, remainingList) =
if remainingList = [] then
[count, current]
else if current = hd remainingList then
helper(current, count + 1, tl remainingList)
else count::current::look_and_say(remainingList)
and look_and_say [] = []
| look_and_say (x::y) = helper(x, 1, y)

Using Mathematica Manipulate function to plot a transfer function

It's my first time asking for help here, I hope someone respond. I was hoping to post images to show the problem I had but I need at least 10 reps to do it. But I hope everyone understand what I'm asking for.
I'm trying to create a manipulate box to plot a transfer function with type in boxes so that I can type in the the transfer function and specify the x and y axis. But the plot itself is not appearing only the axis are
but if i type the code outside of "manipulate" it works.
If you try running this on Mathematica you could probably see the problem I'm having.
My mathematica code is below.
Manipulate[tfplot,
{{tfplot1, 0, "Transfer Function="}},
Delimiter,
{{fmin1, 10, "fmin = "}},
{{fmax1, 10^7, "fmax = "}},
{{ymin1, 1, "ymin = "}},
{{ymax1, 2*10^2, "ymax = "}},
Delimiter,
Row[{
Button["Plot", tfplot = LogLogPlot[Abs[tfplot2[2*Pi*I*f] /. {tfplot2[s_] -> tfplot1}], {f, fmin1, fmax1}, PlotPoints -> 1000, PlotRange -> {{fmin1, fmax1}, {ymin1, ymax1}}, PlotLabel -> "tf Plot"], ImageSize -> 80]
}]
, ControlPlacement -> {Left, Left, Left, Left, Left, Left, Left, Top}]
tfplot3 = (3.333321894500285`*^6 (4.611679331492357`*^6 - 72057.48955456808` s - 4.138291871540356`*^9 s^3 - 3.889993968666704`*^9 s^4 + s^5))/(s^2 (2.606152799059127`*^18 + 4.6278171788297256`*^16 s + 1.0779994813998577`*^14 s^2 + 1.5235290577558628`*^8 s^3 + s^4))
LogLogPlot[Abs[tfplot4[2*Pi*I*f] /. {tfplot4[s_] -> tfplot3}], {f, 10, 10^7}, PlotPoints -> 1000, PlotRange -> {{10, 10^7}, {1, 2*10^2}}, PlotLabel -> "tf Plot"]
Thank you.
Spiderfiq
Edit .. take 2..
Manipulate[
fplot = LogLogPlot[Abs[tfplotf /. s -> 2*Pi*I*f], {f, fmin1, fmax1},
PlotPoints -> 1000, PlotRange -> {{fmin1, fmax1}, {ymin1, ymax1}},
PlotLabel -> "tf Plot"],
{{tfplotf, (3.333321894500285`*^6 (4.611679331492357`*^6 -
72057.48955456808` s - 4.138291871540356`*^9 s^3 -
3.889993968666704`*^9 s^4 +
s^5))/(s^2 (2.606152799059127`*^18 +
4.6278171788297256`*^16 s + 1.0779994813998577`*^14 s^2 +
1.5235290577558628`*^8 s^3 + s^4))
, "Transfer Function="}},
Delimiter,
{{fmin1, 10, "fmin = "}},
{{fmax1, 10^7, "fmax = "}},
{{ymin1, 1, "ymin = "}},
{{ymax1, 2*10^2, "ymax = "}},
Delimiter,
ControlPlacement -> {Left, Left, Left, Left, Left, Left, Left, Top}]
This is some old code I had lying around from my System Dynamics and Controls class.
Manipulate[tf = TransferFunctionModel[eq, s];
BodePlot[tf, GridLines -> Automatic, ImageSize -> 500,
FrameLabel -> {{{"magnitude (db)", None}, {None,
"Bode plot"}}, {{"phase(deg)", None}, {"Frequency (rad/sec)",
None}}},
ScalingFunctions -> {{"Log10", "dB"}, {"Log10", "Degree"}},
PlotRange -> {{{0.1, 100}, Automatic}, {{0.1, 100},
Automatic}}], {eq, (5 s)/(s^2 + 4 s + 25)}]
-Brian

Mathematica GraphPlot with images

I am trying to use GraphPlot function to build a Graph, where each node is an image. I wanted to display the image as my vertex. Does anybody know how to do this?
I tried something like this:
GraphPlot[ Map[If[# > 2.0 , 0, 1] &,
imgDistT, {2}],
VertexRenderingFunction -> (Inset[imgs[[#2]], #1, Center] &) ]
But this does not work.
imgs is my list of images corresponding to each vertex number.
As a sanity check, if i do this:
GraphPlot[
Map[If[# > 2.0 , 0, 1] &, imgDistT, {2}],
VertexRenderingFunction -> (Inset[Text[#2], #1, Center] &) ]
then that works and it shows me the vertex number at each node.
imgs = ExampleData /# ExampleData["TestImage"];
GraphPlot[{1 -> 4, 1 -> 5, 2 -> 3, 2 -> 4, 2 -> 5, 3 -> 4, 3 -> 5},
VertexRenderingFunction -> (Inset[Image[imgs[[#2]], ImageSize -> 100], #1] &)]
Edit
-- Infix notation joke removed --
Two possible issues:
It looks like your graph, Map[If[# > 2.0 , 0, 1] &, imgDistT, {2}], will contain zeroes and ones—but zeroes are invalid indices for the imgs array
The images may not appear properly due to scaling issues—for example, they might be really big only the white portion might be visible. Try specifying an explicit image size.
What is the output of
GraphPlot[Map[If[# > 2.0 , 0, 1] &, imgDistT, {2}],
VertexRenderingFunction -> (Module[{tmp =
Inset[Image[imgs[[#2]], ImageSize -> 10], #1, Center]},
Print[tmp]; tmp] &)]
?

Resources