What is this function syntax doing? [duplicate]

I’m trying to work my way through another person’s notebook. I came across this function definition:

H[cpl_] :=   H[cpl] = Function[y,     Sqrt[(1. + y + RneuT[cpl][y] + RL[cpl]*y^4)/        2./(1 + RneuT[cpl][1]/2. + RL[cpl]/2.)]/y] 

What’s going on here with the H[cpl_] := H[cpl] = … syntax? This appears to be explicitly assigning the return value – which is, itself, a function – to the return value of this function. I can’t find any documentation on this syntax. Could anyone explain what’s going on or give me relevant link?

A Smooth and Round Voronoi Mesh

I want the edges of a VoronoiMesh to be smooth and round. I have found the following code from this answer

arcgen[{p1_, p2_, p3_}, r_, n_] :=   Module[{dc = Normalize[p1 - p2] + Normalize[p3 - p2], cc, th},    cc = p2 + r dc/EuclideanDistance[dc, Projection[dc, p1 - p2]];   th = Sign[      Det[PadRight[{p1, p2, p3}, {3, 3}, 1]]] (\[Pi] -         VectorAngle[p3 - p2, p1 - p2])/(n - 1);   NestList[RotationTransform[th, cc],     p2 + Projection[cc - p2, p1 - p2], n - 1]] roundedPolygon[Polygon[pts_?MatrixQ], r_?NumericQ,    n : (_Integer?Positive) : 12] :=   Polygon[Flatten[    arcgen[#, r, n] & /@      Partition[If[TrueQ[First[pts] == Last[pts]], Most, Identity][pts],       3, 1, {2, -2}], 1]] 

Consider for example, the 3×3 hexagonal mesh (see this question for more details)

L1 = 3; L2 = 3; pts = Flatten[    Table[{3/2 i, Sqrt[3] j + Mod[i, 2] Sqrt[3]/2}, {i, L2 + 4}, {j,       L1 + 4}], 1]; mesh0 = VoronoiMesh[pts]; mesh1 = MeshRegion[MeshCoordinates[mesh0],     With[{a = PropertyValue[{mesh0, 2}, MeshCellMeasure]},      With[{m = 3}, Pick[MeshCells[mesh0, 2], UnitStep[a - m], 0]]]]; mesh = MeshRegion[MeshCoordinates[mesh1],    MeshCells[mesh1, {2, "Interior"}]] 

enter image description here

Using roundedPolygon defined above, I can get what I want with

Graphics[{Directive[LightBlue, EdgeForm[Gray], EdgeThickness -> .001],      roundedPolygon[#, 0.3]} & /@ MeshPrimitives[mesh, 2]] 

enter image description here

This looks good already, but I have the following questions:

  1. Is it possible to fill the gaps between cells automatically? I first thought about setting a Background colour on in Graphics that would match the edge colour. This, however, yields a box look that I want to avoid. I could also change the edge thickness, but this doesn’t seem to scale with the lattice size. Any idea how to solve this? The following picture illustrates these cases.

enter image description here

  1. Is it possible to scale the EdgeThickness with the mesh size?

  2. When I consider a square mesh, given, for example, by pts = Flatten[Table[{i, j}, {i, L2 + 2}, {j, L1 + 2}], 1] and mesh = MeshRegion[MeshCoordinates[mesh0], MeshCells[mesh0, {2, "Interior"}]]

enter image description here

roundedPolygon seems to fail, returning, among others, the error

enter image description here

Any idea how to solve this?

  1. Finally, I wonder if it’s possible to display the mesh as a mesh-type object and avoid using Graphics.

I don’t expect to get an answer to everything, but any ideas or suggestions are welcome.

How to adjust PrecisionGoal and AccuracyGoal to solve my equation using NDSolve[]?

This is a very interesting question. I’m solving a differential equation to get a function z[$ \rho$ ], the expected solution needs to start from $ z[\rho_c]=z[\frac{13}{5}]=\frac{24195890215702774518563237183870329}{8112963841460668169578900514406400}\approx 2.98$ , until when the function $ z[\rho]$ hits on the $ \rho$ -axis. The code is as follows:

precTmp = 4 MachinePrecision; sol = (\[Rho]c = 13/5;    vt = Sqrt[zt^2 - \[Rho]c^2] - zt;     ParametricNDSolveValue[{(1 - z[\[Rho]]^3)^2/z[\[Rho]] +         3/2 z[\[Rho]]^2 Derivative[1][          z][\[Rho]]^2 + (1 - z[\[Rho]]^3) (Derivative[1][z][\[Rho]]^2/           z[\[Rho]] + (z^\[Prime]\[Prime])[\[Rho]]) + (        169 (-(13/5) + zt)^2 (13/5 +            zt)^2 z[\[Rho]]^2 (-3 z[\[Rho]]^2 +            2 (z^\[Prime]\[Prime])[\[Rho]]))/(200 zt^2) == 0,       Derivative[1][z][13/5] == -((13 (2 - (-(169/25) + zt^2)^(3/2)))/(        10 Sqrt[-(169/25) + zt^2])), z[13/5] == Sqrt[-(169/25) + zt^2],       WhenEvent[z[\[Rho]] == 10^-3, "StopIntegration"],       WhenEvent[z[\[Rho]] == 1.1` Sqrt[zt^2 - \[Rho]c^2],        "StopIntegration"]}, z, {\[Rho], \[Rho]c, R + 1}, {zt},      WorkingPrecision -> precTmp, AccuracyGoal -> precTmp/2, MaxSteps -> 10^6,      Method -> "StiffnessSwitching"]); 

Note that we can adjust the parameter precTmp to ramp up the precision. To check the correctness of the solution, it’s natural to plot the solution out, as follows:

ztValue = 24195890215702774518563237183870329/   8112963841460668169578900514406400; Plot[sol[ztValue][\[Rho]], {\[Rho], sol[ztValue]["Domain"][[1, 1]],    sol[ztValue]["Domain"][[1, 2]]}, PlotRange -> All,   WorkingPrecision -> precTmp] 

The first thing I can’t understand is that the shape of the solution $ z[\rho]$ is different when I change the precision, for example, taking precTmp to be 3 MachinePrecision, 5 MachinePrecision, or 6 MachinePrecision, the solution is decreasing and hit on $ \rho$ -axis, but when taking precTmp to be 4 MachinePrecision, 8 MachinePrecision or 12 MachinePrecision, the solution turns out to be increasing. My first question is which one of them is the correct answer?

The second question that I can’t understand is when providing a PrecisionGoal in ParametricNDSolveValue[] function, for example, we take precTmp = 4 MachinePrecision again, and add PrecisionGoal->precTmp, the solution becomes a decreasing function, which is originally an increasing one. I’ve already learned the difference between precision and accuracy, which can somehow be regarded to be equivalent as long as the function doesn’t go to zero. Thus my second question is how to understand the difference brought by the PrecisionGoal set here.

Mathematica returns uneditable long solutions for two simple quadratic equations

I tried to get positive solution(or any solution) of the following two quadratic equations with two variables. My code is:

Solve[(1/8)(-A1+x2+α+x1(-2+β)-2 β x2^2-θ x1^2)==0 &&       (1/16)(A1+3 x2-α-2β x2+x1(-2+3β))^2 - θ x2^2==0, {x1, x2}] 

It shows that there is large output, then i clicked show fulloutput, it took 5 minutes to display …and the result is in weird format, there is only one symbol in each line in the last part, and they are very difficult to identify, i can’t even find where x2 appears

Error while giving initial condition in NDSolve

I am trying to solve following differential equation $ $ \frac{d^2y}{dx^2}+(a+b \frac{2}{\pi}\tan^{-1} x)y=0$ $ with the initial condition $ y(-10)=e^{10i\sqrt{a+b}}$ and $ y'(-10)=-i\sqrt{a+b}e^{10i\sqrt{a+b}}$ .

To implement it I write the following code

s = ParametricNDSolve[{y''[x] + (a + b (2 + 2/Pi ArcTan[x])) y[x] ==  0, y[-10] = Exp[I 10 Sqrt[a + b]], y'[-10] = -I Sqrt[ a + b]*Exp[ I 10 Sqrt[a + b]] }, y, {x, -10, 10}, {a, b}] 

But I am getting an error that ParamatericNDSolve expects equation or list of equations instead of $ e^{10i\sqrt{a+b}}$ in the first argument. Can anyone point me out where am I making the mistake?

Setting different steps for Y-Axes of ListPlot

I am trying to set the scaling interval of my Y-Axes different than Mathematica automatically does. So in steps of 1000 instead of 2000 (see picture)

At the moment I determined following PlotRange:

PlotRange -> {{1, 10}, {0, 8000}} 

enter image description here

Is there a simple option?

To get an overview over the command:

ListPlot [   MapThread[    If[Or[#1[[1]] === 3., #1[[1]] === 8.],       Callout[Tooltip[#1, #2], #2], Tooltip[#1, #2]] &, Annotated2],    FrameLabel -> {"Happiness Score",      "Education Expenditure (per capita)"}, Frame -> True,    GridLines -> Automatic, LabelStyle -> {GrayLevel[0]},    PlotRange -> {{1, 10}, {0, 8000}}] // Framed 

How can I use ThreadingLayer to perform operations with layers of different dimmensions?

Is there a way that I can achieve subtraction between a scalar and a vector within NetGraph[], similar to what happens when I perform {1, 2, 3}-1 to get {0,1,2}? For my purposes, this needs to be done within NetGraph[] for timing and functional reasons. Part of the trouble is that the vector also will be changing in size. Is there already a NetGraph[] layer that has these functionalities?

For those who want a little more background, I am trying to create a filter within my network as part of the loss function, where the data being fed into the network spans over just two days and each day (of the week) is represented as a value between 0 and 1. So Monday is represented as 0.0 and Tuesday as 0.2 and so forth.

I am using SequenceLastLayer[] to get the time of the last bar of data, and am attempting to use this in conjunction with a slightly modified Tanh[] function within ThreadingLayer[], as well as the bars with the other time values whose form will be similar to {.0,.0,.0,...,.2,.2,.2,}. So the one scalar, in this example, would be 0.2 and the vector would be {.0,.0,.0,...,.2,.2,.2,}, and the operation would be:

`{.0,.0,.0,...,.2,.2,.2,} - 0.2` 

This seems like it should be relatively simple, but I have not yet been able to figure it out. Does anyone know what I can do differently?

Here is the code so far:

net = NetGraph[<|"thread" -> ThreadingLayer[#1*#2 &],     "getLastLayer" ->      SequenceLastLayer[]|>, {NetPort["timeInput"] -> {"getLastLayer",       "thread"}, "getLastLayer" -> "thread"}] 

enter image description here

And the code to run it would be similar to:

net[<|"timeInput" -> {0.0,0.0,0.0,...,0.2,0.2,0.2,}|>] 

Holding and dragging mouse to select multiple buttons/cells

Is it possible to create a TogglerBar-type object in which several buttons can be selected/deselected simply by holding the mouse’s left-click button and dragging it over the buttons? Similarly to the multiple selecting option when holding control and the mouse on Windows, but without the drawing box.

It seems that, by default, TogglerBar only selects a button when the left-click button is released, but I’m unaware of any way of doing what I want. Any ideas?

Just to give some context. I’m working with cell dynamics, and I intend to make it easier to select a large group of cells in an epithelium. This is, in some sense, a follow up to a question I made before regarding a “toggleable” Voronoi Mesh.