Tag Info

New answers tagged

0

The MathModelica syntax in the linked paper is obsoleted and not supported by SystemModeler. To create and modify models in SystemModeler from a notebook, use the functions WSMCreateModel, WSMConnectComponents, WSMCreateModelString, WSMConnectComponentsString and WSMSetValues. To simulate use the functions WSMSimulate and WSMSimulateSensitivity. To extract ...


3

Table and Sum scope their variables in the manner of Block. Effectively your code is like this: f[x_] := Block[{i = 5}, x] f /@ {a, i} {a, 5} This is actually a very useful aspect of Table but in this case it is also the source of your problems. Since you must localize the iterators one of the best solutions is the one you reject out of hand which ...


2

If I understand correctly, your problem is that you do not want to have to comb through this block of code manually to find which variables are being used as iterators. Fortunately, you can do this with pattern matching. Say we have this definition: ClearAll@"Global`*"; function[x_] := Module[{}, notanswer = Sum[j^2, {j, 10}]; answer = Table[x, {i, ...


5

Make use of Module's capability to localize variables. f[x_] := Module[{i}, Table[x, {i, 1, 3}]] f[i] {i, i, i} Also, with i localized, you don't need to use distinct iterator names in different iteration constructs. g[x_] := Module[{i, a, b}, a = Table[x, {i, 3}]; b = Table[x^3, {i, 2}]; {a, b}] {g[i], g[a], g[b]} {{{i, i, i}, {i^3, ...


9

Here is one way: Manipulate[Block[{t1, t2, v1, v2, pt, pts}, t1 = {xm, ym}; t2 = {xn, yn}; {v1, v2} = p; pt = {t1, t2} /. NSolve[{(t2 - v2).(t2 - t1) == 0, (t1 - v1).(t2 - t1) == 0, (t1 - v1).(t1 - v1) == r1^2, (t2 - v2).(t2 - v2) == r2^2}, {xm, ym, xn, yn}, Reals]; pts = Select[pt, Sign[(#[[1]] - v1).(#[[2]] - v2)] == 1 &]; ...


8

Warning: Modifying a built-in function is not advised As @m_goldberg already stated, Lookup has Attributes HoldAllComplete, so a workaround will be to remove this Attribute: Edit: As per m_goldberg's recommendation attr = Attributes[Lookup]; Attributes[Lookup] = {}; Now t1 = TempHead[a -> 1, b -> 2, c -> 3]; t2 = TempHead[c -> 3, d -> 4, ...


6

Lookup has the attribute HoldAllComplete, which means the kernel evaluator will not see its arguments and, therefore, will not look at its up-values.


2

ClearAll[base]; base = If[Head[#] === BaseForm, BaseForm[First @ #, #2], BaseForm[##]] &; FoldList[base, 63696, {16, 8, 2}]


2

You can define your own function that works with input the BaseForm of a number, sure: myBase[a_?NumericQ, base_Integer] := BaseForm[a, base]; myBase[a_BaseForm, base_Integer] := BaseForm[ FromDigits[ IntegerString @@ a, Last@a ], base]; so that 63969 // myBase[#, 16] & // myBase[#, 8] & gives you BaseForm[63969,8] but ...


2

My understanding about the execution process of MapThread I find it is easy to understand MapThread by using the inneral built-in function Transpose MapThread[f, {{a, b, c}, {x, y, z}, {u, v, w}}] Apply[f, Transpose[{{a, b, c}, {x, y, z}, {u, v, w}}], {1}] {f[a, x, u], f[b, y, v], f[c, z, w]} The comprehension of Inner Learning from Leonid ...


4

I think of Outer like nikie's answer shows. Here's a similar view of Inner. Think of the arguments in columns. Apply f to each row and g to the result. args = {{a, b, c}, {x, y, z}}; Format[g[e__]] := Column[{g, e}, Dividers -> {None, {False, True, False}}, Alignment -> Center]; Inner[f, Sequence @@ args, g]


6

I think of Outer just like nikie showed. Inner is a generalization of matrix multiplication. I like the picture from the Wikipedia page. To calculate an entry of matrix multiplication, you first pair list entries (a11,b12) and (a12,b22). You "times/multiply" those pairs (a11*b12) and (a12*b22), and then you "plus/add" all the results (a11*b12)+(a12*b22). ...


9

Animated Mathematica Functions contains cool animated illustrations of the way a number of built-in functions work. Among them are Thread Inner: Outer See also: carmullion's video


4

(i = Inner[List, Range@3, Range@3, List]) // MatrixForm; (o = Outer[List, Range@3, Range@3]) // MatrixForm p1 = ListLinePlot[i, Mesh -> All, PlotStyle -> Red, PlotTheme -> "Detailed"]; p2 = ListLinePlot[o, Mesh -> All, PlotStyle -> Blue, PlotTheme -> "Detailed"]; Legended[Show[p2, p1, PlotRange -> All], LineLegend[{Red, Blue}, ...


9

Not sure if that's what you're looking for: This is the image I always have in mind for Outer[f,{a,b,c},{x,y,z}]: args = {{a, b, c}, {x, y, z}}; TableForm[Outer[f, args[[1]], args[[2]]], TableHeadings -> args]


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: ...


4

As others have noted, it's not entirely clear what you want. A possibility is you want to "simplify" the rules so that, on application, all four variables would give numbers. I'll show a way to obtain that and possibly you can modify to suit the actual need. We'll start with the lists of rules and create defining polynomials and also extract the variables ...


1

Expanding on Mr.Wizard's answer, the side effects can be eliminated simply be doing the work inside a function rather than at top-level. list1 = {a -> b + c, d -> b + c}; list2 = {b -> 1, c -> 1}; f[list1_, list2_] := {list1, list2} /. list2 // Values f[list1, list2] {{2, 2}, {1, 1}} Column@{{a, b, c, d}, list1, list2}


3

Update Following the new examples you gave I believe this does what you want: Thread[lists[[All, 1]] -> (lists[[All, 2]] //. lists)] Output: {v[4] -> 2000000000000, v[6] -> 4.92958*10^12, v[15] -> 100000000, v[31] -> 6.*10^11, v[32] -> 0.05, v[33] -> 2000000000000, v[35] -> 400000000000, v[41] -> 1.6*10^12, v[45] -> 1, ...


4

Of course you can use the If also inside the table: Table[If[k != 6, f[k] = g[k], f[k] = 1], {k, 10}] You can also use the If on the right hand side of the assignment: Table[f[k] = If[k != 6, g[k], 1], {k, 10}] If the whole purpose of the table is the assignment, that is you are not really interested in the list it outputs, you can replace Table with ...


4

In addition to RunnyKine's recommendation of If here are two other methods to consider. 1: Unset or restore the value Simply Unset f[6] afterward: Do[f[k] = g[k], {k, 10}]; f[6] =. Array[f, 10] {g[1], g[2], g[3], g[4], g[5], f[6], g[7], g[8], g[9], g[10]} Note f[6] is returned as it is undefined. Or if f[6] already has a value save and restore it: ...


7

You can do this without Table: g[k_] := k^2 + 1 Map[If[# != 6, f[#] = g[#], f[#] = 1] &, Range[10]] {2, 5, 10, 17, 26, 1, 50, 65, 82, 101} You can also use Scan in place of Map (it may be slightly faster for large lists). Note that no result will be displayed, but f will still retain the values stored in it. Scan[If[# != 6, f[#] = g[#], f[#] = ...


5

You can use DisplayFunction for this: Plot[Sin[x], {x, 0, 2 Pi}, DisplayFunction :> (Show[#, Epilog -> Inset["PlotRange is " <> ToString@PlotRange[#], {4, 1}]] &)] Instead of buggy PlotRange and AbsoluteOptions you can use much more reliable plotRange function from this answer: plotRange[plot : (_Graphics | _Graphics3D | ...


7

Not sure how robust it is, but the following seems to work for PlotRange: ClearAll[plt]; plt = Plot[Sin[x], {x, 0, 3 Pi}, PlotRange -> Automatic, PlotLabel -> Dynamic@("PlotRange is " <> ToString@PlotRange[plt])] and ClearAll[plt1]; plt1 = Plot[Sin[x], {x, 0, 3 Pi}, PlotRange -> Automatic, PlotLabel -> ...


1

Pattern matching approach: Clear[replaceWithZero] replaceWithZero[l_List] := With[{allZero = ConstantArray[0, {3, 3}]}, l //. {a___, x_, b___, y_, c___} /; x != allZero && y != allZero && MemberQ[x + y, 2, {2}] :> {a, x, b, allZero, c}]; replaceWithZero /@ tT // MatrixForm Update: scaling this function up to take ...


1

I think I have a no-loops approach to your problem. First, I am using the fact that you only want to compare tT11 with tT12 and tT13, not with any of the matrices on the other rows of the super matrix. So create a function that operates on a single list of matrices, and then Map that function onto your matrix of matrices, tT. myfun[a : {__?MatrixQ}] /; ...


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 ...


1

I find the question confusing but piggybacking on Karsten's apparently correct answer you might consider: p = {{4, -1}, {5, -2}, {1, 5}}; list1 = Subsets[p, {2}]; f4[{a_, b_}] := Null[, a, b,][[ Sign[a - b].{1, -1} ]] # - {0, 1} & /@ Tally @ Join[p, f4 /@ list1] {{{4, -1}, 1}, {{5, -2}, 2}, {{1, 5}, 0}} A count of Null represents any case that ...


1

Not sure if I well understand the criterion, because for much more points my results are different from those obtained with f1 and f2 suggested by @Karsten 7. However, they agree with those obtained using dominateCount written by RunnyKine. So, I would have your suggestions about my solution. dominate[l : {{_, _} ..}] := Module[{a, b}, a = Sort[l]; ...


2

A slightly different approach Clear[dominateCount] dominateCount[points : {_, _} ..] := Module[{pairs, dominatePick, pickedPoints}, pairs = Subsets[{points}, {2}]; dominatePick[p1_List, p2_List] := Switch[p1 - p2, {_?NonNegative, _?NonPositive}, p1, {_?NonPositive, _?NonNegative}, p2, _, Null]; pickedPoints = dominatePick @@@ pairs; ...


3

Here a very direct approach to the problem: list1 = Subsets[{p1, p2, p3}, {2}]; f = Module[{x1 = #[[1, 1]], y1 = #[[1, 2]], x2 = #[[2, 1]], y2 = #[[2, 2]]}, If[x1 > x2 && y1 < y2, #[[1]], #[[2]]] ] &; and then Tally[f /@ list1] However, I'm not sure if the alternative is correct, as it wasn't specified. If you ...



Top 50 recent answers are included