wolfram-mathematica

Using nested slots (#)

為{幸葍}努か 提交于 2019-11-29 07:30:52
问题 Suppose I want to construct something like Array[#1^#2 == 3 &, {3, 3}] And now I want to replace the "3" with a variable. I can do, for example: f[x_] := Array[#1^#2 == x &, {x, x}] The question is: Is there a way using only slots and & as the functional notation? 回答1: How about Map[Last, #] & /@ Array[#1^#2 == #3 &, {#, #, #}] &[3] Horrendously ugly element extraction, and very interestingly Map[Last, #]& gives me a different result than Last /@ . Is this due to the fact that Map has

Preventing “Plus” from rearranging things

偶尔善良 提交于 2019-11-29 07:19:04
The following code creates a diagram of a certain calculation. My problem is that even though the terms in denominator are in a nice order, after applying Plus on it, they get rearranged arbitrarily. Any suggestions how to force the original order to be kept? (source: yaroslavvb.com ) r[i_] := Floor[(i - 1)/n] + 1; c[i_] := Mod[i, n, 1]; adj[a_, b_] := Abs[r[a] - r[b]] + Abs[c[a] - c[b]] == 1; indsetQ[s_] := Not[Or @@ (adj @@@ Subsets[s, {2}])]; indsets[k_] := Select[Subsets[Range[n^2], {k}], indsetQ]; twoColorGraph[g_, seen_, lbl_] := Module[{radius = .22}, vcoords = # -> {c[#], n - r[#]} & /

Adaptive gridlines

独自空忆成欢 提交于 2019-11-29 07:00:03
I want to use gridlines to create an effect of millimeter graphing paper on a 2d graph, to show how multi-variable function depends on 1 variable. The scales of different variables differ a lot, so my naive approach (that I have used before) does not seem to work. Example of what I have at the moment: << ErrorBarPlots` Cmb[x_, y_, ex_, ey_] := {{N[x], N[y]}, ErrorBar[ex, ey]}; SetAttributes[Cmb, Listable]; ELP[x_, y_, ex_, ey_, name_] := ErrorListPlot[ Cmb[x, y, ex, ey], PlotRange -> FromTo[x, y], PlotLabel -> name, Joined -> True, Frame -> True, GridLines -> GetGrid, ImageSize -> {600} ] Both

Why does Default behave like this?

落花浮王杯 提交于 2019-11-29 06:44:52
问题 One may set a Default value for the arguments of a function: Default[f] = 5; And then use: f[a_, b_.] := {a, b} f[1, 2] f[1] {1, 2} {1, 5} This creates the following Values: DefaultValues[f] DownValues[f] {HoldPattern[Default[f]] :> 5} {HoldPattern[f[a_, b_.]] :> {a, b}} From this one might think that the value 5 is not fixed in the definition of f , but addresses the DefaultValues assignment. However, if we change the DefaultValues , either directly or using: Default[f] = 9; DefaultValues[f]

Generating Combinations

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 05:59:51
Every time I have to do this I "invent" a different way. Time to standardize. I suspect there is some default command I overlooked ready to do this, so I am sorry in advance if the question is too trivial. What is the better (memory, performance) way to get: combinations[{1,2,3},2] = {{1,2},{1,3},{2,3}} with arbitrary elements in the input list, of course. cah Subsets[{1, 2, 3}, {2}] is the built-in way. Before Subsets was added as a core function, the Combinatorica function KSubsets was available. Needs["Combinatorica`"] KSubsets[{1, 2, 3}, 2] (* {{1, 2}, {1, 3}, {2, 3}} *) Combinatorica

custom function with non-standard evaluation (behaves like Table)

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 05:16:16
I'd like a function AnyTrue[expr,{i,{i1,i2,...}}] which checks if expr is True for any of i1,i2... It should be as if AnyTrue was Table followed by Or@@% , with the difference that it only evaluates expr until first True is found. Short-circuiting part is optional, what I'd really like to know is the proper way to emulate Table 's non-standard evaluation sequence. Update 11/14 Here's a solution due to Michael, you can use it to chain "for all" and "there exists" checks SetAttributes[AllTrue, HoldAll]; SetAttributes[AnyTrue, HoldAll]; AllTrue[{var_Symbol, lis_List}, expr_] := LengthWhile[lis,

Efficient way to pick/delete a list of rows/columns in a matrix in Mathematica

微笑、不失礼 提交于 2019-11-29 05:03:33
This question is in a way a continuation of the question I asked here: Simple way to delete a matrix column in Mathematica to which @belisarius and @Daniel provided very helpful answers. What I am generally trying to do is to extract from a matrix A specific lines and columns OR what remains after what those specified are removed. So this can be formally writtewn as, find TakeOperator and Drop Operator such that: TakeOperator[A,{i1,..,ip},{j1,...,jq}]=(A[[ik]][[jl]]) (1<=k<=p, 1<=l<=q) = Table[A[[ik]][[jl]],{k,p},{l,q}] We note Ic={i'1,...,i'p'}= Complement [{1,..., Length[A] },{i1,...,ip}];Jc

Extending cell definition to CellFrameLabels definition

爱⌒轻易说出口 提交于 2019-11-29 04:40:38
I'm in the process of creating a notebook that contains a style to write documents. I would like Mathematica to behave similar to LaTeX in the sense that when I write a "Definition" cell then it will write "Definition [Chapter#].[Definition#]" . To see what I mean do the following. In an empty notebook create a cell and modify the style to "Chapter" . You can do this by selecting the cell and the going to Format->Style->Other , enter "Chapter" . Now go to Format->Edit StyleSheet... . Enter Chapter in the input box. This will generate a cell labeled Chapter. Select that cell, and click on Cell-

Seeing truncated messages in Mathematica

╄→尐↘猪︶ㄣ 提交于 2019-11-29 04:17:33
Is it possible to see full version of a Message that got truncated? IE, I see something along the lines of 0.105309,0.394682,<<20>>,<<20>>,<<20>>,0.394631 in the Messages window. I'm guessing <<20>> represents omitted parts, how do I get the whole thing? The function called is FindMaximum on a problem with 50 variables. Update: Simon's answer seems to work for general messages, also I found an approach that's specific to capturing the FindMaximum "not a real number" message. To get the point which causes FindMaximum to fail with "not a real number" message you can do the following (redefining

How do I stop recursion happening in a Format/Interpretation Mathematica construction?

前提是你 提交于 2019-11-29 04:06:30
问题 This question follows on from the answer given by Michael Pilat in Preventing “Plus” from rearranging things. There he defined a custom + notation using Format[myPlus[expr__]] := Row[Riffle[{expr}, "+"]] The problem with this is you can't copy and paste the output (although % or Out[] still works). To get around this you should use the Interpretation type facility which allows an expression to be displayed as one thing, but interpreted as another when supplied as input. My modification of