Why does Julia not optimize this code when C++ (LLVM) can? - julia

When using a C++ compiler with LLVM version 6.0.0, the following code
bool isEven(int n) {
bool ret = true;
for (int i = 0; i < n; i ++) {
ret = !ret;
}
return ret;
}
emits the LLVM IR
define zeroext i1 #_Z6isEveni(i32) local_unnamed_addr #0 !dbg !7 {
call void #llvm.dbg.value(metadata i32 %0, metadata !14, metadata !DIExpression()), !dbg !18
call void #llvm.dbg.value(metadata i8 1, metadata !15, metadata !DIExpression()), !dbg !19
call void #llvm.dbg.value(metadata i32 0, metadata !16, metadata !DIExpression()), !dbg !20
%2 = icmp slt i32 %0, 1, !dbg !21
%3 = and i32 %0, 1, !dbg !23
%4 = icmp eq i32 %3, 0, !dbg !23
%5 = or i1 %4, %2, !dbg !23
ret i1 %5, !dbg !24
}
declare void #llvm.dbg.value(metadata, metadata, metadata) #1
attributes #0 = { nounwind readnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { nounwind readnone speculatable }
See: https://godbolt.org/z/oPBFey
This is functionally equivalent to the following implementation:
julia> isEven(n::Int) = rem(n, 2) != 0
isEven (generic function with 1 method)
julia> #code_llvm debuginfo=:none isEven(7)
define i8 #julia_isEven_18796(i64) {
top:
%1 = trunc i64 %0 to i8
%2 = and i8 %1, 1
%3 = xor i8 %2, 1
ret i8 %3
}
julia>
However, the original C++ implementation ported to Julia results in a very different LLVM IR:
julia> function isEven(n::Int)
out = true
for i in 0:n-1
out = !out
end
return out
end
isEven (generic function with 1 method)
julia> #code_llvm debuginfo=:none isEven(7)
define i8 #julia_isEven_18793(i64) {
top:
%1 = add i64 %0, -1
%2 = icmp sgt i64 %1, -1
br i1 %2, label %L8.L12_crit_edge, label %L25
L8.L12_crit_edge: ; preds = %top
%min.iters.check = icmp ult i64 %0, 128
br i1 %min.iters.check, label %scalar.ph, label %vector.ph
vector.ph: ; preds = %L8.L12_crit_edge
%n.vec = and i64 %0, -128
br label %vector.body
vector.body: ; preds = %vector.body, %vector.ph
%index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
%vec.phi = phi <32 x i8> [ <i8 1, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0, i8 0>, %vector.ph ], [ %3, %vector.body ]
%vec.phi8 = phi <32 x i8> [ zeroinitializer, %vector.ph ], [ %4, %vector.body ]
%vec.phi9 = phi <32 x i8> [ zeroinitializer, %vector.ph ], [ %5, %vector.body ]
%vec.phi10 = phi <32 x i8> [ zeroinitializer, %vector.ph ], [ %6, %vector.body ]
%3 = xor <32 x i8> %vec.phi, <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>
%4 = xor <32 x i8> %vec.phi8, <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>
%5 = xor <32 x i8> %vec.phi9, <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>
%6 = xor <32 x i8> %vec.phi10, <i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1, i8 1>
%index.next = add i64 %index, 128
%7 = icmp eq i64 %index.next, %n.vec
br i1 %7, label %middle.block, label %vector.body
middle.block: ; preds = %vector.body
%bin.rdx = xor <32 x i8> %vec.phi8, %vec.phi
%bin.rdx14 = xor <32 x i8> %5, %bin.rdx
%bin.rdx15 = xor <32 x i8> %6, %bin.rdx14
%rdx.shuf = shufflevector <32 x i8> %bin.rdx15, <32 x i8> undef, <32 x i32> <i32 16, i32 17, i32 18, i32 19, i32 20, i32 21, i32 22, i32 23, i32 24, i32 25, i32 26, i32 27, i32 28, i32 29, i32 30, i32 31, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
%bin.rdx16 = xor <32 x i8> %bin.rdx15, %rdx.shuf
%rdx.shuf17 = shufflevector <32 x i8> %bin.rdx16, <32 x i8> undef, <32 x i32> <i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
%bin.rdx18 = xor <32 x i8> %bin.rdx16, %rdx.shuf17
%rdx.shuf19 = shufflevector <32 x i8> %bin.rdx18, <32 x i8> undef, <32 x i32> <i32 4, i32 5, i32 6, i32 7, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
%bin.rdx20 = xor <32 x i8> %bin.rdx18, %rdx.shuf19
%rdx.shuf21 = shufflevector <32 x i8> %bin.rdx20, <32 x i8> undef, <32 x i32> <i32 2, i32 3, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
%bin.rdx22 = xor <32 x i8> %bin.rdx20, %rdx.shuf21
%rdx.shuf23 = shufflevector <32 x i8> %bin.rdx22, <32 x i8> undef, <32 x i32> <i32 1, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
%bin.rdx24 = xor <32 x i8> %bin.rdx22, %rdx.shuf23
%8 = extractelement <32 x i8> %bin.rdx24, i32 0
%cmp.n = icmp eq i64 %n.vec, %0
br i1 %cmp.n, label %L25, label %scalar.ph
scalar.ph: ; preds = %middle.block, %L8.L12_crit_edge
%bc.resume.val = phi i64 [ %n.vec, %middle.block ], [ 0, %L8.L12_crit_edge ]
%bc.merge.rdx = phi i8 [ %8, %middle.block ], [ 1, %L8.L12_crit_edge ]
br label %L12
L12: ; preds = %L12, %scalar.ph
%value_phi2 = phi i8 [ %bc.merge.rdx, %scalar.ph ], [ %9, %L12 ]
%value_phi3 = phi i64 [ %bc.resume.val, %scalar.ph ], [ %11, %L12 ]
%9 = xor i8 %value_phi2, 1
%10 = icmp eq i64 %value_phi3, %1
%11 = add i64 %value_phi3, 1
br i1 %10, label %L25, label %L12
L25: ; preds = %L12, %middle.block, %top
%value_phi6 = phi i8 [ 1, %top ], [ %9, %L12 ], [ %8, %middle.block ]
ret i8 %value_phi6
}
julia> versioninfo()
Julia Version 1.3.1
Commit 2d5741174c (2019-12-30 21:36 UTC)
Platform Info:
OS: macOS (x86_64-apple-darwin18.6.0)
CPU: Intel(R) Core(TM) i7-7920HQ CPU # 3.10GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-6.0.1 (ORCJIT, skylake)
julia>
Can anyone explain why Julia is not able to produce the same IR as a C++ compiler for essentially the same code with almost the same version of LLVM?

The short answer is:
Julia and C++ are different language with different semantics and different compilers.
The different semantics means that different optimizations at legal.
It would take some close looking to see if this was one that is legal in C++ but illegal in Julia.
I would be surprised if it was.
The different compilers mean the compilers do different things.
C++ compilers have had decades and probably hundreds of millions of dollars of developer time put into them (Even if a lot of it was donated by open source volunteers); even a younger compiler like Clang is still able to build directly on decades of proven ideas from older compilers like GCC.
The Julia compiler was first started in 2012.
Much fewer hours have gone into it. Indeed I don't think it actually had its own optimizer at all until v0.6, which was 2017.
LLVM does have an optimizer, that both Julia and Clang use.
But they use it differently, they have different passes enabled and they provide it with different information (because of different semantics).
Plus you are looking at the code before LLVM has run.
(so might want to look at the assembly instread).
The LLVM versions being the same between the two only matter for what instructions exist -- not for what optizations LLVM does since you are looking at the code before that gets to run.

Related

Combining legends in mathematica Grid[]

I am trying to plot 2 figures that both share identical legends in a grid in Mathematical. Here is an example of the code:
Grid[{{Plot[{x + 1, 2 x + 1}, {x, 0, 10},
PlotStyle -> {{RGBColor[0.880722, 0.611041, 0.142051],
Dashed}, {RGBColor[0.880722, 0.611041, 0.142051], colors}},
PlotLegends -> {a, b}, ImageSize -> 400]},
{Plot[{-x + 10, -2 x + 10}, {x, 0, 10},
PlotStyle -> {{RGBColor[0.880722, 0.611041, 0.142051],
Dashed}, {RGBColor[0.880722, 0.611041, 0.142051], colors}},
PlotLegends -> {a, b}, ImageSize -> 400]}}]
I want to only have one legend, instead of two yet I can't find a clean way to do it with legend since there is a dashed line.
Also added ImagePadding to align the y axes.
Legended[
Grid[{{Plot[{x + 1, 2 x + 1}, {x, 0, 10},
PlotStyle -> {{RGBColor[0.880722, 0.611041, 0.142051],
Dashed}, {RGBColor[0.880722, 0.611041, 0.142051], colors}},
ImageSize -> 400,
ImagePadding -> {{14, Automatic}, {Automatic, Automatic}}]},
{Plot[{-x + 10, -2 x + 10}, {x, 0, 10},
PlotStyle -> {{RGBColor[0.880722, 0.611041, 0.142051],
Dashed}, {RGBColor[0.880722, 0.611041, 0.142051], colors}},
ImageSize -> 400,
ImagePadding -> {{14, Automatic}, {Automatic, Automatic}}]}}],
Placed[LineLegend[{{RGBColor[0.880722, 0.611041, 0.142051],
Dashed}, RGBColor[0.880722, 0.611041, 0.142051]}, {a, b}], Below]]

How can I add a horizontal line (equilibrium) to Vector Plot in Mathematica

Equilibrium solution for y'=3-2y is at y=3/2 and I would like to add the line y=3/2 to direction field plotted using VectorPlot function. How to do?
The additional code below does it.
points = {{0, 0}, {1, 0}, {2, 0}, {0, 1}, {1, 1}, {2, 1}, {0, 2}, {1,
2}, {2, 2}};
datplot =
VectorPlot[{1, 3 - 2 y}, {x, 0, 2}, {y, 0, 2},
VectorPoints -> points, VectorScale -> {Automatic, Automatic, None},
Epilog -> {Red, PointSize[Medium], Point[points]}];
fitplot = Plot[y = 3/2, {y, 0, 2}];
{Show[datplot, fitplot]}
Mary A. Marion
Change
Epilog -> {Red, PointSize[Medium], Point[points]}
to
Epilog -> {Red, PointSize[Medium], Point[points], Line[{{0,3/2},{2,3/2}}]}
The following code will compute the solution*:
points = {{0, 0}, {1, 0}, {2, 0}, {0, 1}, {1, 1}, {2, 1},
{0, 2}, {1,2}, {2, 2}};
datplot =
VectorPlot[{1, 3 - 2 y}, {x, 0, 2}, {y, 0, 2},
VectorPoints -> points, VectorScale -> {Automatic, Automatic, None},
Epilog -> {Red, PointSize[Medium], Point[points],
Line[{{0, 3/2}, {2, 3/2}}]}]
See Bill's input above.

How to input function so it can be graphed correctly

Currently I am trying to graph the sin(x) function and a function named myPolys which is the taylor polynomial of sin(x) as it is equal to
myPolys =
Table[Table[(-1)^((j - 1)/2) x^j/Factorial[j], {j, 1, h, 2}], {h, 1,
19, 2}];
How can I use manipulate to graph both functions so that each part of myPolys is graphed
My graphing code so far:
Manipulate[Plot[{Sin[x], myPolys[[n]]}, {x, -10, 10},
PlotRange -> {-5, 5}], {n, 1, Length[myPolys], 1}];
currently for each iteration of n, myPolys is graphed as separate
x and then x & -(x^3)/3! and then x & -(x^3)/3! & x^5/5! (all are plotted separate o the same graph )
The graph I'm trying to achieve is that for n=1 sin(x) should be plotted and x from myPoly should be plotted, for n=2 it continues and graphs x-(x^3/3!) (instead of plotting, for n=2, x and -x^3/3! seperately) and so on and so forth until n reaches 10.
My graph at the moment:
The graph I'm hoping for:
myPolys = Table[Sum[(-1)^((j - 1)/2) x^j/Factorial[j],
{j, 1, h, 2}], {h, 1, 19, 2}];
Manipulate[Plot[{Sin[x], Evaluate#Take[myPolys, n]},
{x, -10, 10}, PlotRange -> {-5, 5}], {n, 1, Length[myPolys], 1}]
Or, in more functional style.
Clear[myPolys]
myPolys[n_] := Table[Sum[(-1)^((j - 1)/2) x^j/Factorial[j],
{j, 1, h, 2}], {h, 1, 2 n - 1, 2}]
Manipulate[Plot[{Sin[x], Evaluate#myPolys[n]},
{x, -10, 10}, PlotRange -> {-5, 5}], {n, 1, 10, 1}]
And with legend.
myLabels[n_] := Table[Sum[(-1)^((j - 1)/2) x^j/ToString[j] <> "!",
{j, 1, h, 2}], {h, 1, 2 n - 1, 2}]
Manipulate[Plot[{Sin[x], Evaluate#myPolys[n]},
{x, -10, 10}, PlotRange -> {-5, 5},
PlotLegends -> Placed[PointLegend[
Rest#Array[ColorData[97], n + 1], HoldForm /# myLabels[n],
LegendMarkers -> {"\[Tilde]", 30}], Left]], {n, 1, 10, 1}]
I suppose you know there is the built in Series you can use..
Manipulate[m = n;
Show[{
Plot[Sin[x], {x, -10, 10}, PlotRange -> {-5, 5},
PlotStyle -> {Thick, Red}],
Plot[Evaluate[
Accumulate[List ## Normal#Series[Sin[x], {x, 0, m}]]], {x, -10, 10}]}],
{{n, 3}, 3, 25, 2}]

How to set the step of plotting by one of axes in Wolfram Mathematica

I have a data such as list of points x,y,z. I added interpolation by splines as
f = Interpolation[{#[[1 ;; 2]], #[[3]]} & /# data, Method -> "Spline"]
And after all I try to plot
`Show[Plot3D[f[x, y], {x, 10000, 10000000}, {y, 10, 50}, Mesh -> 15,
MeshFunctions -> {#3 &}, PlotRange -> {0, 0.0011},
PlotRangePadding -> {0.001, 0.0003}, PlotStyle -> Opacity[0.7],
ColorFunction -> "Rainbow",
AxesStyle -> {Black, Black, Black}],
Graphics3D[{Red, PointSize[.016], Point[data]}]]`
Can't post image.
I use such data as
data = {{10000, 10, 0.000000208191701}, {10000, 20, 0.000000416383402}, {10000, 30, 0.00000066886188}, {10000, 40,0.000000832854501}, {10000, 50, 0.000001040870809}, {100000, 10,0.000002081829313}, {100000, 20, 0.000004163483234}, {100000, 30,0.000006245400245}, {100000, 40, 0.000008327229558}, {100000, 50,0.000010409058871}, {1000000, 10, 0.000020818731618}, {1000000,20, 0.000041636761666}, {1000000, 30,
0.000062455405588}, {1000000, 40, 0.00008327361103}, {1000000, 50,
0.000104092254952},{10000000, 10, 0.000208475750285}, {10000000,
20, 0.000416951237469}, {10000000, 30,
0.000625426900044}, {10000000, 40, 0.000833902387228}, {10000000,
50, 0.001042377962108}}
And I have the question, how can I pull the graph in the Z axis by setting of other step of plotting?
I have changed the aspect ratio from the default, and added z-axis tick values.
You could simply use Ticks -> {Automatic, Automatic, {0, 0.0002, 0.0004, 0.0006, 0.0008, 0.001}} but I added some formatting. FindDivisions is useful for less straightforward cases.
Show[Plot3D[f[x, y], {x, 10000, 10000000}, {y, 10, 50}, Mesh -> 15,
MeshFunctions -> {#3 &}, PlotRange -> {0, 0.0011},
PlotRangePadding -> {0.001, 0.0003}, PlotStyle -> Opacity[0.7],
ColorFunction -> "Rainbow", AxesStyle -> {Black, Black, Black},
Ticks -> {Automatic, Automatic,
{#, NumberForm[#, {3, 4}]} & /# N#FindDivisions[{0, 0.001}, 5]},
AspectRatio -> 1, ImageSize -> 300],
Graphics3D[{Red, PointSize[.016], Point[data]}]]
Edit
Adding sub-ticks:-
Show[Plot3D[f[x, y], {x, 10000, 10000000}, {y, 10, 50}, Mesh -> 15,
MeshFunctions -> {#3 &}, PlotRange -> {0, 0.0011},
PlotRangePadding -> {0.001, 0.0003}, PlotStyle -> Opacity[0.7],
ColorFunction -> "Rainbow", AxesStyle -> {Black, Black, Black},
Ticks -> {Automatic, Automatic,
DeleteCases[
Flatten[{{#1, NumberForm[#1, {3, 4}]}, {#2, ""}, {#3, ""}, {#4, ""}} & ###
Partition[N#FindDivisions[{0, 0.001}, 20], 4, 4, {1, 1}, Null], 1],
{Null, _}]},
AspectRatio -> 1, ImageSize -> 300],
Graphics3D[{Red, PointSize[.016], Point[data]}]]
Adding Box Ratios
Show[Plot3D[f[x, y], {x, 10000, 10000000}, {y, 10, 50}, Mesh -> 15,
MeshFunctions -> {#3 &}, PlotRange -> {0, 0.0011},
PlotRangePadding -> {0.001, 0.0003}, PlotStyle -> Opacity[0.7],
ColorFunction -> "Rainbow", AxesStyle -> {Black, Black, Black},
Ticks -> {Automatic, Automatic,
DeleteCases[
Flatten[{{#1, NumberForm[#1, {3, 4}]}, {#2, ""}, {#3, ""}, {#4, ""}} & ###
Partition[N#FindDivisions[{0, 0.001}, 20], 4, 4, {1, 1}, Null], 1],
{Null, _}]}, AspectRatio -> 1, ImageSize -> 1200],
Graphics3D[{Red, PointSize[.016], Point[data]}],
BoxRatios -> {1, 1, 3},
BaseStyle -> FontSize -> 36]

Upgrade from Mathematica 5 to Mathematica 7

I am trying to upgrade some equations that currently are written against Mathematica 5 to get them to work in Mathematica 7.
F = Graphics[
ContourPlot[
x^2 + (2)*y^2 + (-1)*((1)/(3))*x, {x, -1, 1}, {y, -1, 1},
ContourShading -> False, ContourStyle -> {RGBColor[1, 0, 1]},
Contours -> 20, PlotPoints -> 100]];
G = ParametricPlot[{Cos[u], Sin[u]}, {u, 0, 2*Pi},
PlotStyle -> {RGBColor[0, 174/255, 239/255]}];
H = DeleteCases[F, {x_, y_} /; (x^2 + y^2 > 1), 5];
Show[{H, G}, AspectRatio -> Automatic, Frame -> False, Axes -> True,
AxesOrigin -> {0, 0}, AxesLabel -> {x, y}, Ticks -> None]
It should look like this:
, but id does look like this:
and it gives the following error:
Thread::tdlen: Objects of unequal length in
{{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}}+{{},<<21>>,{}}
cannot be combined. >>
and if you hover over the image you get the following in a tooltip:
The specified setting for the option GraphicsBoxOptions, PlotRange cannot be used.
Coordinate index X is out of range for the enclosing GraphicsComplex
there are a lot of those Coordinate ones all with different coordinates.
And the second one I'm having difficulty with is:
P1 = {{(5)/10*Cos[u]*Cos[v], (5)/10*Sin[u]*Cos[v], (5)/10*Sin[v]}, {u,
0, 2*Pi}, {v, -Pi/2, Pi/2}};
P2 = {{(5)/10*Cos[u], 0, (5)/10*Sin[u]}, {u, 0, 2*Pi}};
P3 = {{(5)/10*Cos[u], (5)/10*Sin[u], 0}, {u, 0, 2*Pi}};
P4 = {{0, (5)/10*Cos[u], (5)/10*Sin[u]}, {u, 0, 2*Pi}};
U = {P1, P2, P3, P4};
XL = {{-1, 1}, {-1, 1}, {-1, 1}};
XV = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
XS = {"x", "y", "z"};
f[a_, b_, c_] := {a, 1.1*b[[2]]*c};
g[a_, b_] := {(b*# &) /# a};
T = Text ### MapThread[f, {XS, XL, XV}];
A = Line ### MapThread[g, {XL, XV}];
F = (ParametricPlot3D[Evaluate[Evaluate ## #]][[1]] &) /# U;
OPT = {Boxed -> False, Axes -> False, BoxRatios -> {1, 1, 1}, PlotRange -> XL,
ViewPoint -> {2.4, 1.3, 2},
DisplayFunction ->
Identity};
L = {LightSources -> {{{0.1, 0, 1}, RGBColor[0.68, 0.88, 0.96]}}};
gr1 = (Show[Graphics3D[#], OPT, L] &) /# {{EdgeForm[], F}, {Dashing[{0.03, 0.03}],
GrayLevel[0.7], A}, {T}};
gr2 = DeleteCases[Graphics[Show[Graphics3D[{A, EdgeForm[], F}], OPT,
RenderAll -> False]], {__, _Polygon}, 3];
Show[{gr1, gr2}, AspectRatio -> Automatic]
This one should look like: , but does look like:
which and it gives these errors:
ParametricPlot3D::write: Tag Plus in x^2+y^2 is Protected. >>
Graphics3D::optx : Unknown option RenderAll
Graphics3D::optx : Unknown option LightSources
If I then remove the Unknown options, those errors disappear but it still looks wrong:
Also, if you hover over the last image in mathematica you get the following message repeated several times in a tooltip
Times is not a Graphics3D primitive or directive
Second one, great circles partly hidden, axes partly dashed:
r = 1.01/2; d = 1/(100 r);
v1 = Riffle[
Table[r { Cos[\[Phi]], Sin[\[Phi]], -d}, {\[Phi], 0, 2 \[Pi], (
2 \[Pi])/40.}],
Table[r { Cos[\[Phi]], Sin[\[Phi]], d}, {\[Phi], 0, 2 \[Pi], (
2 \[Pi])/40.}]];
v2 = Riffle[
Table[-r { Cos[\[Phi]], -d, Sin[\[Phi]]}, {\[Phi], 0, 2 \[Pi], (
2 \[Pi])/40.}],
Table[-r { Cos[\[Phi]], d, Sin[\[Phi]]}, {\[Phi], 0, 2 \[Pi], (
2 \[Pi])/40.}]];
v3 = Riffle[
Table[r { -d, Cos[\[Phi]], Sin[\[Phi]]}, {\[Phi], 0, 2 \[Pi], (
2 \[Pi])/40.}],
Table[r { d, Cos[\[Phi]], Sin[\[Phi]]}, {\[Phi], 0, 2 \[Pi], (
2 \[Pi])/40.}]];
lx = {{{1, 0, 0}, {2, 0, 0}}}/2;
ly = {{{0, 1, 0}, {0, 2, 0}}}/2;
lz = {{{0, 0, 1}, {0, 0, 2}}}/2;
A2 = Line ### {lx, ly, lz, -lx, -ly, -lz};
Graphics3D[
{
FaceForm[None, Black], EdgeForm[],
GraphicsComplex[#,
Table[Polygon[Mod[{i, i + 1, i + 3, i + 2}, Length[v], 1]], {i,
1, Length[v] - 1, 2}]] & /# {v1, v2, v3}, {Opacity[0.75],
RGBColor[0.68`, 0.88`, 0.96`], Sphere[{0, 0, 0}, 1/2]},
{Dashing[{0.02, 0.02}], Black, A}, T, A2
},
Boxed -> False,
Lighting -> {{"Directional", RGBColor[0.68`, 0.88`, 0.96`],
ImageScaled#{0.1, 0, 1}}}, BoxRatios -> {1, 1, 1},
PlotRange -> {{-1, 1}, {-1, 1}, {-1, 1}},
BaseStyle -> FontSize -> 14, ViewPoint -> {2.4`, 1.3`, 2},
ViewVertical -> {0, 0, 1}
]
It is a little hard to tell from the picture what you want for the second graphic. Please try this and tell me what is right and what is lacking.
P1 = {{(5)/10*Cos[u]*Cos[v], (5)/10*Sin[u]*Cos[v], (5)/10*Sin[v]}, {u,
0, 2*Pi}, {v, -Pi/2, Pi/2}};
P2 = {{(5)/10*Cos[u], 0, (5)/10*Sin[u]}, {u, 0, 2*Pi}};
P3 = {{(5)/10*Cos[u], (5)/10*Sin[u], 0}, {u, 0, 2*Pi}};
P4 = {{0, (5)/10*Cos[u], (5)/10*Sin[u]}, {u, 0, 2*Pi}};
U = {P1, P2, P3, P4};
XL = {{-1, 1}, {-1, 1}, {-1, 1}};
XV = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
XS = {"x", "y", "z"};
f[a_, b_, c_] := {a, 1.1*b[[2]]*c};
g[a_, b_] := {(b*# &) /# a};
T = Text ### MapThread[f, {XS, XL, XV}];
A = Line ### MapThread[g, {XL, XV}];
F = ParametricPlot3D[##, Mesh -> False][[1]] & ### U;
OPT = {Boxed -> False, Axes -> False, BoxRatios -> {1, 1, 1},
PlotRange -> XL, ViewPoint -> {2.4, 1.3, 2}};
L = Lighting -> {{"Directional",
RGBColor[0.68, 0.88, 0.96], {{5, 5, 4}, {5, 5, 0}}}};
gr1 = Graphics3D[#, OPT, L] & /# {{Opacity[0.5], F},
{Dashing[{0.03, 0.03}], GrayLevel[0.7], A},
{T}};
Show[gr1]
Here is another take that may be closer to the original. You lose the v7 ability to rotate the graphic with this.
gr1 = Rasterize[Graphics3D[#, OPT, L],
Background -> None] & /# {F, {Dashing[{0.03, 0.03}],
GrayLevel[0.7], A}, T};
Show[gr1]
First one:
ContourPlot[x^2 + (2)*y^2 + (-1)*((1)/(3))*x, {x, -1, 1}, {y, -1, 1},
ContourShading -> False, ContourStyle -> {RGBColor[1, 0, 1]},
Contours -> 20, RegionFunction -> (#1^2 + #2^2 <= 1 &),
BoundaryStyle -> Blue]
[Edit]
Second one, using the given values for A and T:
Show[
ParametricPlot3D[{(5)/10*Cos[u]*Cos[v], (5)/10*Sin[u]*Cos[v], (5)/10*
Sin[v]}, {u, 0, 2*Pi}, {v, -Pi/2, Pi/2},
MeshFunctions -> {#1 &, #2 &, #3 &}, PlotStyle -> Opacity[0.75],
Mesh -> {{0}, {0}, {0}}, MeshStyle -> Black, BoundaryStyle -> Black,
Boxed -> False, Axes -> False, BoxRatios -> {1, 1, 1},
PlotRange -> {{-1, 1}, {-1, 1}, {-1, 1}},
ViewPoint -> {2.4`, 1.3`, 2},
Lighting -> {{"Directional", RGBColor[0.68`, 0.88`, 0.96`],
ImageScaled#{0.1, 0, 1}}}],
Graphics3D[{{Dashing[{0.03, 0.03}], Black, A}, {T}}]
]
I think the axes will need to be done as separate interior/exterior segments.
The great circles show through in this version; preventing that will require drawn partial curves instead of mesh lines, if that's required.

Resources