Tag Info

New answers tagged

5

It seems me that the answers of mathe and Yves Klett do not meet expectations of the author. The latter is as much as I have got it, to have a short analytical expression for the solution. Probably the author has an intention to use the result further in some analytical calculations, or to do something comparable. Am I right? If yes, one should first of ...


5

Solve[L == (3 W)/2 + 3/2 Sqrt[4 A^2 Pi^2 + W^2] - Sqrt[ 6 A^2 Pi^2 + 3 W^2 + 5 W Sqrt[4 A^2 Pi^2 + W^2]]/Sqrt[2], W, Quartics -> False] or Solve[L == (3 W)/2 + 3/2 Sqrt[4 A^2 Pi^2 + W^2] - Sqrt[ 6 A^2 Pi^2 + 3 W^2 + 5 W Sqrt[4 A^2 Pi^2 + W^2]]/Sqrt[2], W, Reals]


1

One shotgun approach is to sic Simplify or FullSimplify onto your solution: sol1 = Solve[ L == (3 W)/2 + (3 Sqrt[4 A^2 Pi^2 + W^2])/2 - Sqrt[6 A^2 Pi^2 + 3 W^2 + 5 W Sqrt[4 A^2 Pi^2 + W^2]]/Sqrt[2], W]; sol2 = Simplify[sol1]; LeafCount /@ {sol1, sol2} ByteCount /@ {sol1, sol2} {3849, 3077} {111720, 92840} (Note: FullSimplify is still ...


3

I wanted to let everyone know what happened to me in case it prevents the stress I'm feeling right now. I was worried about the changes in the way dates are handled as I have many lines of code with date features as I am backtesting investments. I decided to see what happens and if I could fix any issues with great inputs above. I made sure not to ...


5

Description ? at the beginning of a line is the short from of Information. Given a pattern that matches multiple Symbols Information returns a list of them in alphabetical (canonical) order, in columns top to bottom and left to right. You are therefore asking how you can modify the behavior of this System function. You would need some way of storing the ...


2

First, I should say that I could find no examples of using RegionPlot3D with regions in the documentation. It works on some regions, not on others, and in this case runs longer than one wants to wait. It runs nonstop because Reduce[Exists[{u, v}, x - Cos[u] == 0 && y - Cos[v] - Sin[u] == 0 && z - Sin[v] == 0 && 0 <= u ...


0

I think you may find value in this rule: sequentialQ[x_List] := x == Range[x[[1]], x[[-1]]] rule = HoldPattern[Plus[s : F_[_, j___] ..]] /; sequentialQ @ {s}[[All, 1]] :> (HoldForm[Sum[F[i, j], {i, ##}]] & @@ {Min@#, Max@#} &[{s}[[All, 1]]]); Test: F[1.0, q, r] + F[1.5, q, r] + F[2, q, r] + F[2.7, q, r] + F[3.0, q, r] /. rule


2

Here is a different sort of answer, but very V10-style. The only logical expression however is Element, so I'm afraid this will fall short. Clear[regFn, regFn`mesh]; regFn`mesh[polyh_] := regFn`mesh[polyh] = ConvexHullMesh@PolyhedronData[polyh, "VertexCoordinates"] regFn[polyh_] := With[{region = regFn`mesh[polyh]}, {##} ∈ region &] This ...


2

I suppose they are nice....They check that a point is inside each facet plane. # -> PolyhedronData[#, "RegionFunction"] & /@ {"Octahedron", "Dodecahedron", "Icosahedron"} (* {"Octahedron" -> (2 (#1 + #3) <= Sqrt[2] + 2 #2 && 2 (#1 + #2 + #3) <= Sqrt[2] && 2 (#2 + #3) <= Sqrt[2] + 2 #1 && 2 #3 <= ...


0

You can use Mod with a non-integer second argument: f[a_] := Ceiling[Mod[a, 2 \[Pi]]/\[Pi]] To generalize you can make a Periodic operator as follows: Periodic[T_, offset_: 0][f_] := f@*((Mod[#, T] - offset) &) f2pi = Periodic[2 \[Pi]][Ceiling[#/\[Pi]] &] f3pi = Periodic[3 \[Pi], 0.1][Ceiling[#/\[Pi]] &] quadratic[x_] := (x/\[Pi])^2; parabola ...


2

You can use a combination of Floor, Floor[x, a] gives the greatest multiple of a less than or equal to x and Mod, Mod[m, n] gives the remainder on division of m by n to get f[x_] := 1 + Floor[Mod[x, 2 π]/π]


3

You can also use the built-in function SquareWave: ClearAll[f]; f[x_] := SquareWave[{2, 1}, x/(2 Pi)]; Plot[f[x], {x, 0, 10 Pi}, Ticks -> {Pi Range[10], Automatic}, ExclusionsStyle -> Automatic, PlotRange -> {0, 3}, ImageSize -> 400] Update: another alternative: Use ListInterpolation with options InterpolationOrder->0 and ...


1

Here is a method that uses UnitStep and Sin to generate the square wave. Sin produces the periodicity and UnitStep maps the sinusoid into a square-wave. This method will be faster than Which. f[x_] := 1 + UnitStep[Sin[x + Pi]] Plot[f[x], {x, -10, 10}, Exclusions -> None, PlotStyle -> Thick]


1

Here's one way to make your function periodic: f[t_] := Which[0 <= t < Pi, 1, Pi <= t < 2 Pi, 2, t < 0, f[t + 2 Pi], t > 2 Pi, f[t - 2 Pi]] For example: Plot[f[t], {t, -10, 10}] This method works well for any function you care to use (not just square waves). For instance, f[t] can be linear in one half and quadratic in the second ...


2

In mathematics, a set can be write in two(or more) form: one is {3x | x in [0,1]}, the other {x | P(x)}, where P is called a propositional function or predicate. Since Mathematica have a great power of dealing with quantifiers, once you can write a set in the second form above, you can do plenty of things and tricks that you would have thought to have ...


6

The answer is 0, if the question is what is the value of Mean[fit["FitResiduals"]] and fit is a linear, least-squares fitted model. data = WeatherData["London", "Temperature", {{2004, 1, 1}, {2013, 12, 31}, "Day"}]; normaldata = Partition[Reverse[data["Values"][[All, 1]]], 2, 1]; fit = LinearModelFit[SetPrecision[normaldata, Infinity], x, x, ...


1

Something like this: m4 = Quiet[Table[a[[i, j]], {i, 4}, {j, 4}]]; soln = With[{det = Simplify[Det[m4]]}, Compile[{{a, _Real, 2}}, det]]; soln[RandomReal[1., {4, 4}]] -0.036117495644621564` But Det is quite efficient as it is, I would think.


11

foo = With[{f = #0}, (# /. {p___, a, b, c, q___} :> Join[{p, "abc"}, f @ {q}])] & lst = {1, 2, a, b, c, 3, {4, a, b, c}, 5}; foo@lst (* {1, 2, "abc", 3, {4, "abc"}, 5} *) This works because With automatically renames the patterns used in RuleDelayed since both are scoping constructs. Other constructs can be used as well such as RuleDelayed itself: ...


3

You can "inject" that variable by grabbing it with With. It then gets put into the body of the With verbatim, hence it gets inside the Function in the desired form. f2[expr_] := With[{var = First@Variables[expr]}, expr /. var -> # &] f2[x^2] (* Out[79]= x^2 /. x -> #1 & *) This can also be done in the way you had tried, with a bit of work ...


1

f[a_, b_, c_] = a + b + c; list = {2, 3}; f[a, Sequence @@ list] (* 5 + a *)


2

I really don't know what all your complicated code is supposed to do and I'm too tired to find out. However from your problem statement this should help: f = Sin[#] Sin[#2] &; a = Array[f, {6, 6}, 0]; min = Min[a] Position[a, min] Sin[2] Sin[5] {{3, 6}, {6, 3}} For values rather than coordinates consider SortBy or for somewhat better ...


11

Let's start by taking a look at the compiled form of one of our queries: Dataset`CompileQuery[Query @ First @ spans] (* Dataset`WithOverrides@*Checked[Slice[205 ;; 313], Identity] *) We can see that the operation is not implemented directly in terms of part. Indeed, there are three components: Dataset`WithOverrides, GeneralUtilities`Checked and ...


1

As defined, eislminx2 returns a list rather than a value. I modified its definition to use First (only?) value. I rationalized your equations so that they do not limit the precision of the calculations. In addition to specifying a value for epsilon, h needs a value. Do you know any constraints on values of n1 or n2 (region of interest)? eislexpl[epsilon_, ...


1

Updating mutually dependent functions at each iteration It seems like one important aspect which leads to the circular dependency problem here (pointed out earlier) is that you want to update your function definitions for tmed, tmedpred, ttf, med, and tpred at each iteration. Your code uses global rules to create and define your functions (e.g. f[x_] := ...


3

t4[x_,t_]:=Evaluate[t1[x,t]+t2[x,t]]


3

The following set of expressions, which is taken from the relevant part of your code and simplified for discussion purposes, causes infinite recursion. tpred[x_, t_] := .6 tmedpred[x, t] + .4 med[x, t]; tmed[x_?NumericQ, t_?NumericQ] := tpred[x, t]; tmedpred = tmed tpred is defined in terms of tmedpred. tmed is defined in terms of tpred. Then tmedpred ...


5

It's fun to use Associations as a circular linked list, which automatically handles the cyclic nature of these ranges: Clear@monthRange monthRange[start_, end_] := Module[{monthsLL, head}, monthsLL = Fold[ <|#2 -> #|> &, Reverse[DataPaclets`CalendarDataDump`MonthList["Gregorian"] ~Join~ {monthsLL}] ]; head = ...


12

The separation-of-variables solution you quoted has two indices appearing in it: n and j (the subscripts of the coefficient $A_{nj}$). Here, n is azimuthal mode order, i.e. it counts the number of nodes along the direction in which the polar-angle $\theta$ varies (divided by 2). The index j is needed because the wave is supposed to satisfy the boundary ...


5

1) Get the list of days and months. Note that days are represented in MMA as symbols (and not strings as months are), hence the use of ToString to make them in a consistent type with the list of months (credit to @Mr.Wizard for this tip). monthList=DateValue[{2014,#,1},"MonthName"]&/@Range[12] (* ...


10

For full ranges There is the function DayRange that can be used for this purpose, but not in the same simple way like CharacterRange. For the days: DayName /@ DayRange[Today, Today ~DatePlus~ {{1, "Week"}, {-1, "Day"}}] {Wednesday, Thursday, Friday, Saturday, Sunday, Monday, Tuesday} For the months: DateValue[#, "MonthName"] & /@ DayRange[Today, ...


3

If you use ToString, make sure you specify InputForm (compare ToString[1/x] v.s. ToString[1/x, InputForm]). Why not something like this? pureify[f_, x_] := Function @@ {f /. x -> #} Table[InverseFunction[pureify[foo, x]][x], {foo, {Sin[x], Log[x], Sqrt[x]}}] (* {ArcSin[x], E^x, x^2} *)


2

Here's my approach to determine the number of slots in the pure function without counting those in any sub-functions. That means MapIndexed[#1^#2 &, #1] & (per @TeakeNutma's comment in @JohnMcGee's answer) will only return one slot. I do this by deleting all inner Functions before counting the slots: Clear[slotCount] ...


1

Thanks to @hieron suggestion to start with the "pure" heads I found this solution: GetHeads[fun_] := ToExpression@First@StringSplit[ToString[fun], "["] f = GetHeads /@ {Sin[x], Log[x], Sqrt[x]} {Sin, Log, Sqrt} Table[InverseFunction[foo[#] &][x], {foo, f}] {ArcSin[x], E^x, x^2}


1

Without "Head-Substitution", you may achieve it: inv = InverseFunction /@ {Sin, Log, Sqrt} x // inv // Through out: {ArcSin[x], E^x, x^2} Edit1: Yes, to get rid of the arguments I just wanted to suggest you inv = InverseFunction /@ ToExpression /@ (StringTake[#, {1, -4}] & /@ToString /@ {Sin[x], Log[x], Sqrt[x]}) x // inv // Through But I ...


3

It's difficult to answer this question definitively, without more detail on the type of function that you're after. As you refer to the PiecewiseExpand function, I'm rather guessing that you're looking for a single, piecewise linear function that passes through the points. If so, perhaps this works: affineFormula[{{x1_, y1_}, {x2_, y2_}}, x_] := y1 + ...


6

The docs specify that the domain should (usually) be Reals or Integers. These are keywords. You probably want the "domain" to be specified as a constraint. Maximize[{ Abs[f[{p1, p2, p3, p4, p5}, {0.5, l2}]], {p1 + p2 + p3 + p4 + p5 == 1 && p1 >= 0 && p2 >= 0 && p3 >= 0 && p4 >= 0 && p5 >= 0 ...


5

is it possible to specify which range that slot takes from? The short answer is "no" (not counting solutions with named arguments since they aren't Slots) but if you really, really want to do it you can write it like I've done below, then #1 refers to the inner Map and #2 refers to the outer Map: Map[ Map[ {Sin[#], Cos[#2]} & @@ # &, ...


6

You can nest Functions to accomplish what I believe you want to: f = i \[Function] {Sin[i], Cos[#]} & /@ list[[i ;;]]; Array[f, 3] // Column {{Sin[1],Cos[a]},{Sin[1],Cos[b]},{Sin[1],Cos[c]}} {{Sin[2],Cos[b]},{Sin[2],Cos[c]}} {{Sin[3],Cos[c]}} This looks nicer in the Notebook: Other methods include: # /. i_ :> ({Sin[i], Cos[#]} & /@ ...


7

My first suggestion would be to localize the outer Slot, like this: With[{indx = #}, {Sin[indx], Cos[#]} & /@ list[[indx ;;]]] & /@ Range[3] (* {{{Sin[1], Cos[a]}, {Sin[1], Cos[b]}, {Sin[1], Cos[c]}}, {{Sin[2], Cos[b]}, {Sin[2], Cos[c]}}, {{Sin[3], Cos[c]}}} *) You could also rework the process to get the full set of tuples of ...


6

As @Mr.Wizard has pointed out, your question isn't well specified. However, a pure function always needs a minimum number of arguments, otherwise an error message is thrown: #1 + #2 &[a] Function::slotn: Slot number 2 in #1+#2& cannot be filled from (#1+#2&)[a] >> a + #2 So finding the minimum number of required arguments of a pure ...


13

Your question is not well specified for several reasons: Pure functions accept a flexible number of arguments #1 + #2 &[a, b, c, d] a + b It is common for some arguments to not be used: #1 + #3 & @@ {a, b, c, d} a + c SlotSequence includes all arguments after the given position: +##3 & @@ {a, b, c, d} c + d Without ...


1

Try Length@Union@Cases[f, _Slot, Infinity] 2 The Union eliminates duplicates


1

Because the latter is equivalent to {1,2,3,4,5}[[2]] = 0 which is invalid. The former stays as listCopy[[2]] = 0 You can't change (i.e. assign to) parts of a literal like this. You can change (i.e. assign to) a variable. Generally, f[x_] := x[[2]] will substitute the value of x directly. So will With[{x=...}, x[[2]] ]. In contrast, ...


8

This is not an answer. It is just a very long comment. Both a simple manually operated drill press and a computer-controlled five-axis omni-mill can drill a hole through a piece of bar stock. And both will do the actual drilling in about the same amount of time. If one hole in one bar is all you want, then you will accomplish the job much faster with the ...


4

Setting b = 1 from the start, your equation can be rewritten as (3 k)/(3 + a k^2) == Tan[k] This has multiple solutions for k for any given value of a. Here's e.g. the plot of the LHS and the RHS for a = 1; the intersections are the solutions: Plot[ { (3 k)/(3 + k^2), Tan[k] }, {k, -10, 10}, Exclusions -> Tan[k] == 0, PlotPoints -> 500 ] ...


4

I stand by @MrWizard's answer. The natural way is with named functions. If it is evaluated, an alternative is anything that prevents Slot[1] from appearing literally. Personally I like Identity[Slot][1] more than Slot@@{1}. If you insist on "protecting" a slot, the only way I know is with another function, so this would work. {#, First[# &]} & /@ ...


5

You can also use Block for this: Block[{slot = #}, {#, slot} & /@ {1, 2}] {{1, #1}, {2, #1}} This works because Block implements dynamic scoping, so the symbol slot is not replaced by #1 until slot is evaluated (i.e. after the Function has been created and mapped over the list). As pointed out by Mr. Wizard, this means that if slot is never ...


11

Using Function with a named parameter, as halmir showed, is the standard way to do this, however anything that prevents a literal Slot[1] from appearing in the body of the Function will work. Inactive as chuy showed is one possibility, but I find this cleaner: {#, Slot @@ {1}} & /@ {1, 2} {{1, #1}, {2, #1}} If the body will not be evaluated you ...


10

Maybe something like (using Inactive): Activate[{#, Inactive[Slot][1]}] & /@ {1, 2} (* {{1, #1}, {2, #1}} *)


12

It depends on what you want to do with result, but you could try like this: Function[{x}, {x, #}] /@ {1, 2} {{1, #1}, {2, #1}}



Top 50 recent answers are included