Dummy indices and constraints in Mathematica - wolfram-mathematica

I would like that Mathematica simplifies an expression according to some constraints. For example:
M = p.k p.k
given the constraint:
p(mu) * p(nu) = \delta(mu,nu)
should give
M = k^2
But I don't know how to specify dummy indices, and I am also not quite sure where I should place the constraint itself. In an argument of Simplify?

In[1]:= FilePrint["dummyindices.m"]
<<HighEnergyPhysics`FeynCalc`
Print[" "];
{$AL[1], $AL[2]} = {mu, nu}; (* $AL are predefined dummy indices *)
M = SP[p,k] * SP[p,k]; (* this defines p.k * p.k *)
SP[k, k] = k2; (* abbreviate the scalar product k^2 by k2 *)
constraint = FeynCalcInternal[
FourVector[p,mu] FourVector[p,nu] -> MetricTensor[mu, nu]
]; (* this is one way of specifying the constraint *)
(* use the Uncontract function http://www.feyncalc.org/FeynCalcBook/Uncontract/*)
Print["uncontracting : ", FCF[ tmp = Uncontract[M, k, Pair->All, Unique->False]]];
Print["contract and using the constraint : ",
FeynCalcExternal # Contract[tmp /. constraint]
]
In[2]:= <<dummyindices.m
Loading FeynCalc from /home/rolfm/HighEnergyPhysics
FeynCalc 8.1.0 Type ?FeynCalc for help or visit http://www.feyncalc.org/
$PrePrint is set to FeynCalcForm. Use FI and FC to change the display format.
Loading FeynArts, see www.feynarts.de for documentation
FeynArts 3.4 patched for use with FeynCalc
uncontracting : k[mu] k[nu] p[mu] p[nu]
contract and using the constraint : k2

Related

mathematica, picking our linear terms from an equation

so I'm trying to pick out the linear terms in an expression - for example if I say
eqn = dy/dt + y == y^2
, dy/dt+y is linear and y^2 is non linear.
I know in this case I can just use
eqn[[1]] to pull out the lhs and have that be my linear terms, but is there some way I can use a string pattern or something to get the linear parts of any entered equation?
This is some quick solution I came up with. You may need to change this for your purposes, but maybe it is a possibility. First the terms are converted into a string. This is then searched for "+" or "-" and separated. The individual terms are tested for linearity.
terms = y + y^2 - Sqrt[y];
string = ToString[terms, InputForm];
split = StringSplit[string, {"+", "-"}];
Do[
(*Take term i and convert the string to an expression*)
exp = ToExpression[split[[i]]];
Print[exp];
(*Test for Additivity: f(x+y)= f(x)+f(y)*)
pexp = exp /. {y -> y + c};(*f(x+y)*)
cexp = exp /. {y -> c};(*f(y)*)
test1 = pexp - (exp + cexp) // Simplify;(*f(x+y)-(f(x)+f(y))*)
(*Test for Homogeneity: f(a*x) = a*f(x)*)
aexp = exp /. {y -> a*y};(*f(a*x)*)
test2 = a*exp - aexp // Simplify;(*a*f(x)-f(a*x)*)
If[test1 == 0 && test2 == 0,
Print["linear"]]
, {i, 1, Length[split]}]

Sort Function in OCaml not working

New to OCaml and learning I am. I wrote the function below. Would you say this function is OK? Well I get an error but does the algorithm makes sense? And how can I correct it.
let rec sort l =
match l with
[] -> []
|h::t -> insert h (sort t)
;;
let rec insert x l =
match l with
[] -> [x]
|h::t ->
if x <= h
then x :: h :: t
else h :: insert x t
;;
sort [3; 2; 8; 4; 1];;
I get in my terminal:
Error: Unbound value sort
In the code you give here, it's insert that's not defined when you use it.
If I put the definition of insert first, it works fine for me. It seems like good code as far as I can tell (though not a particularly fast sort).
I would try starting up your OCaml from scratch again. You probably have some old definitions that are confusing things.
I figured this out myself. I should have made the order of functions so that insert comes before sort :)
(* Sort a list *)
let rec insert x l =
match l with
[] -> [x]
|h::t ->
if x <= h
then x :: h :: t
else h :: insert x t
;;
let rec sort l =
match l with
[] -> []
|h::t -> insert h (sort t)
;;
sort [3; 2; 8; 4; 1];;
The sort function is dependent on the insert function and to OCaml, calling the sort function makes no sense because the it doesn't know the insert function just yet. So changing the order of function definition fixes the problem.

Minimizing chunks in a matrix

Suppose I have the following matrix:
The matrix can be broken down into chunks such that each chunk must, for all rows, have the same number of columns where the value is marked true for that row.
For example, the following chunk is valid:
This means that rows do not have to be contiguous.
Columns do not have to be contiguous either, as the following is a valid chunk:
However, the following is invalid:
That said, what is an algorithm that can be used to select chunks such that the minimal number of chunks will be used when finding all the chunks?
Given the example, above, the proper solution is (items with the same color represent a valid chunk):
In the above example, three is the minimal number of chunks that this can be broken down into.
Note that the following is also a valid solution:
There's not a preference to the solutions, really, just to get the least number of chunks.
I thought of counting using adjacent cells, but that doesn't account for the fact that the column values don't have to be contiguous.
I believe the key lies in finding the chunks with the largest area given the constraints, removing those items, and then repeating.
Taking that approach, the solution is:
But how to traverse the matrix and find the largest area is eluding me.
Also note, that if you want to reshuffle the rows and/or columns during the operations, that's a valid operation (in order to find the largest area), but I'd imagine you can only do it after you remove the largest areas from the matrix (after one area is found and moving onto the next).
You are doing circuit minimization on a truth table. For 4x4 truth tables, you can use a K map. The Quine-McCluskey algorithm is a generalization that can handle larger truth tables.
Keep in mind the problem is NP-Hard, so depending on the size of your truth tables, this problem can quickly grow to a size that is intractable.
This problem is strongly related to Biclustering, for which there are many efficient algorithms (and freely available implementations). Usually you will have to specify the number K of clusters you expect to find; if you don't have a good idea what K should be, you can proceed by binary search on K.
In case the biclusters don't overlap, you are done, otherwise you need to do some geometry to cut them into "blocks".
The solution I propose is fairly straightforward, but very time consuming.
It can be decomposed in 4 major steps:
find all the existing patterns in the matrix,
find all the possible combinations of these patterns,
remove all the incomplete pattern sets,
scan the remaining list to get the set with the minimum number of elements
First of, the algorithm below works on either column or row major matrices. I chose column for the explanations, but you may swap it for rows at your convenience, as long as it remains consistent accross the whole process.
The sample code accompanying the answer is in OCaml, but doesn't use any specific feature of the language, so it should be easy to port to other ML dialects.
Step 1:
Each column can be seen as a bit vector. Observe that a pattern (what you call chunk in your question) can be constructed by intersecting (ie. and ing) all the columns, or all the rows composing it, or even a combinations. So the first step is really about producing all the combinations of rows and columns (the powerset of the matrix' rows and columns if you will), intersecting them at the same time, and filter out the duplicates.
We consider the following interface for a matrix datatype:
module type MATRIX = sig
type t
val w : int (* the width of the matrix *)
val h : int (* the height ........ *)
val get : t -> int -> int -> bool (* cell value getter *)
end
Now let's have a look at this step's code:
let clength = M.h
let rlength = M.w
(* the vector datatype used throughought the algorithm
operator on this type are in the module V *)
type vector = V.t
(* a pattern description and comparison operators *)
module Pattern = struct
type t = {
w : int; (* width of thd pattern *)
h : int; (* height of the pattern *)
rows : vector; (* which rows of the matrix are used *)
cols : vector; (* which columns... *)
}
let compare a b = Pervasives.compare a b
let equal a b = compare a b = 0
end
(* pattern set : let us store patterns without duplicates *)
module PS = Set.Make(Pattern)
(* a simple recursive loop on #f #k times *)
let rec fold f acc k =
if k < 0
then acc
else fold f (f acc k) (pred k)
(* extract a column/row of the given matrix *)
let cr_extract mget len =
fold (fun v j -> if mget j then V.set v j else v) (V.null len) (pred len)
let col_extract m i = cr_extract (fun j -> M.get m i j) clength
let row_extract m i = cr_extract (fun j -> M.get m j i) rlength
(* encode a single column as a pattern *)
let col_encode c i =
{ w = 1; h = count c; rows = V.set (V.null clength) i; cols = c }
let row_encode r i =
{ h = 1; w = count r; cols = V.set (V.null rlength) i; rows = r }
(* try to add a column to a pattern *)
let col_intersect p c i =
let col = V.l_and p.cols c in
let h = V.count col in
if h > 0
then
let row = V.set (V.copy p.rows) i in
Some {w = V.count row; h = h; rows = row; clos = col}
else None
let row_intersect p r i =
let row = V.l_and p.rows r in
let w = V.count row in
if w > 0
then
let col = V.set (V.copy p.cols) i in
Some { w = w; h = V.count col; rows = row; cols = col }
else None
let build_patterns m =
let bp k ps extract encode intersect =
let build (l,k) =
let c = extract m k in
let u = encode c k in
let fld p ps =
match intersect p c k with
None -> l
| Some npc -> PS.add npc ps
in
PS.fold fld (PS.add u q) q, succ k
in
fst (fold (fun res _ -> build res) (ps, 0) k)
in
let ps = bp (pred rlength) PS.empty col_extract col_encode col_intersect in
let ps = bp (pred clength) ps row_extract row_encode row_intersect in
PS.elements ps
The V module must comply with the following signature for the whole algorithm:
module type V = sig
type t
val null : int -> t (* the null vector, ie. with all entries equal to false *)
val copy : t -> t (* copy operator *)
val get : t -> int -> bool (* get the nth element *)
val set : t -> int -> t (* set the nth element to true *)
val l_and : t -> t -> t (* intersection operator, ie. logical and *)
val l_or : t -> t -> t (* logical or *)
val count : t -> int (* number of elements set to true *)
val equal : t -> t -> bool (* equality predicate *)
end
Step 2:
Combining the patterns can also be seen as a powerset construction, with some restrictions: A valid pattern set may only contain patterns which don't overlap. The later can be defined as true for two patterns if both contain at least one common matrix cell.
With the pattern data structure used above, the overlap predicate is quite simple:
let overlap p1 p2 =
let nullc = V.null h
and nullr = V.null w in
let o v1 v2 n = not (V.equal (V.l_and v1 v2) n) in
o p1.rows p2.rows nullr && o p1.cols p2.cols nullc
The cols and rows of the pattern record indicate which coordinates in the matrix are included in the pattern. Thus a logical and on both fields will tell us if the patterns overlap.
For including a pattern in a pattern set, we must ensure that it does not overlap with any pattern of the set.
type pset = {
n : int; (* number of patterns in the set *)
pats : pattern list;
}
let overlap sp p =
List.exists (fun x -> overlap x p) sp.pats
let scombine sp p =
if overlap sp p
then None
else Some {
n = sp.n + 1;
pats = p::sp.pats;
}
let build_pattern_sets l =
let pset l p =
let sp = { n = 1; pats = [p] } in
List.fold_left (fun l spx ->
match scombine spx p with
None -> l
| Some nsp -> nsp::l
) (sp::l) l
in List.fold_left pset [] l
This step produces a lot of sets, and thus is very memory and computation intensive. It's certainly the weak point of this solution, but I don't see yet how to reduce the fold.
Step 3:
A pattern set is incomplete if when rebuilding the matrix with it, we do not obtain the original one. So the process is rather simple.
let build_matrix ps w =
let add m p =
let rec add_col p i = function
| [] -> []
| c::cs ->
let c =
if V.get p.rows i
then V.l_or c p.cols
else c
in c::(add_col p (succ i) cs)
in add_col p 0 m
in
(* null matrix as a list of null vectors *)
let m = fold (fun l _ -> V.null clength::l) [] (pred rlength) in
List.fold_left add m ps.pats
let drop_incomplete_sets m l =
(* convert the matrix to a list of columns *)
let m' = fold (fun l k -> col_extract m k ::l) [] (pred rlength) in
let complete m sp =
let m' = build_matrix sp in
m = m'
in List.filter (fun x -> complete m' x) l
Step 4:
The last step is just selecting the set with the smallest number of elements:
let smallest_set l =
let smallest ps1 ps2 = if ps1.n < ps2.n then ps1 else ps2 in
match l with
| [] -> assert false (* there should be at least 1 solution *)
| h::t -> List.fold_left smallest h t
The whole computation is then just the chaining of each steps:
let compute m =
let (|>) f g = g f in
build_patterns m |> build_pattern_sets |> drop_incomplete_sets m |> smallest_set
Notes
The algorithm above constructs a powerset of a powerset, with some limited filtering. There isn't as far as I know a way to reduce the search (as mentioned in a comment, if this is a NP hard problem, there isn't any).
This algorithm checks all the possible solutions, and correctly returns an optimal one (tested with many matrices, including the one given in the problem description.
One quick remark regarding the heuristic you propose in your question:
it could be easily implemented using the first step, removing the largest pattern found, and recursing. That would yeld a solution much more rapidly than my algorithm. However, the solution found may not be optimal.
For instance, consider the following matrix:
.x...
.xxx
xxx.
...x.
The central 4 cell chunck is the largest which may be found, but the set using it would comprise 5 patterns in total.
.1...
.223
422.
...5.
Yet this solution uses only 4:
.1...
.122
334.
...4.
Update:
Link to the full code I wrote for this answer.

about plotting process -- a further question about "A problem in Mathematica 8 with function declaration"

Related A problem in Mathematica 8 with function declaration
Clear["Global`*"]
model = 4/Sqrt[3] - a1/(x + b1) - a2/(x + b2)^2 - a3/(x + b3)^4;
fit = {a1 -> 0.27, a2 -> 0.335, a3 -> -0.347, b1 -> 4.29, b2 -> 0.435,
b3 -> 0.712};
functionB1[x_] = model /. fit;
functionB2[x_] := model /. fit;
The evaluation difference between functionB1 and functionB2 can be revealed by Trace command in mma, as below:
functionB1[Sqrt[0.2]] // Trace
functionB2[Sqrt[0.2]] // Trace
I have no question about functionB1. what puzzles me is that because functionB2[Sqrt[0.2]] doesn't even gives a numeric result but gives a function of x 4/Sqrt[3] - 0.335/(0.435 + x)^2 + 0.347/(0.712 + x)^4 - 0.27/(
4.29 + x), and then how its plot Plot[functionB2[Sqrt[x]], {x, 0, 1}] is possible?
I mean when you run Plot[functionB2[Sqrt[x]], {x, 0, 1}], what happens inside mma is:
x takes a number, say, 0.2, then 0.2 is finally passed to functionB2, but functionB2 gives a function, not a number. Then how is the following figure generated?
And its trace result ( Plot[functionB2[Sqrt[x]], {x, 0, 1}] // Trace ) seems very unreadable. I wonder the clear plotting process of functionB2. Can anybody show it?
thanks~ :)
SetDelayed acts as a scoping construction. Arguments are localized if necessary. Any variables that explicitly match the arguments are bound within this scope, others are not.
In[78]:= a[x_] := x^2 + b
b = x^4;
(* the first x^2 is explicitly present and bound to the argument.
The x in x^4 present via b is not bound *)
In[80]:= a[x]
Out[80]= x^2 + x^4 (* this is what you would expect *)
In[81]:= a[y]
Out[81]= x^4 + y^2 (* surprise *)
In[82]:= a[1]
Out[82]= 1 + x^4 (* surprise *)
So, what you could do is one of two things:
Use Evaluate: functionB2[x_] := Evaluate[model /. fit];
Make dependence of model on x explicit:
In[68]:= model2[x_] =
4/Sqrt[3] - a1/(x + b1) - a2/(x + b2)^2 - a3/(x + b3)^4;
In[69]:= functionB3[x_] := model2[x] /. fit;
In[85]:= functionB3[Sqrt[0.2]]
Out[85]= 2.01415
Edit because of question update
Because of your definition of functionB2 any argument value yields the same result, as explained above:
In[93]:= functionB2[1]
Out[93]= 4/Sqrt[3] - 0.335/(0.435 + x)^2 + 0.347/(0.712 +
x)^4 - 0.27/(4.29 + x)
In[94]:= functionB2["Even a string yields the same ouput"]
Out[94]= 4/Sqrt[3] - 0.335/(0.435 + x)^2 + 0.347/(0.712 +
x)^4 - 0.27/(4.29 + x)
However, this expression contains x's and therefore it can get a numerical value if we provide a numerical value for x:
In[95]:= functionB2["Even a string yields the same ouput"] /. x -> 1
Out[95]= 2.13607
Well, this basically what Plot does too. This is why you still get a plot.
The definition:
functionB2[x_] := model /. fit
is an instruction to Mathematica to replace all future occurrences of an expression that looks like functionB2[x_] with the result of substituting the value of the argument for every occurrence of x in the expression model /. fit. But there are no occurrences of x in model /. fit: the only symbols in that expression are model and fit (and, technically, ReplaceAll). Therefore, the definition returns a fixed result, model /. fit, irrespective of the argument. Indeed, the definition could just simply be:
functionB2a[] := model /. fit
If you plot functionB2a[], you will get the same result as if you plotted functionB2[anything]. Why? Because Plot will evaluate that expression while varying the symbol x over the plot range. It so happens that model /. fit evaluates to an expression involving that symbol, so you get the exhibited plot.
Now consider functionB1:
functionB1[x_] = model /. fit
It too says to replace all occurrences of x on the right-hand side -- but this time the right-hand side is evaluated before the definition is established. The result of evaluating model /. fit is an expression that does contain the symbol x, so now the definition is sensitive to the passed argument value. The net result is as if the function were defined thus:
functionB1a[x_] := 4/Sqrt[3]-0.335/(0.435+x)^2+0.347/(0.712+x)^4-0.27/(4.29+x)
So, if you plot functionB1[Sqrt[x]], the Plot command will see the expression:
4/Sqrt[3]-0.335/(0.435 +Sqrt[x])^2+0.347/(0.712 +Sqrt[x])^4-0.27/(4.29 +Sqrt[x])
Formal Symbols
When establishing definitions using SetDelayed, the name of the formal argument (x in this case) is independent of any occurrences of the same symbol outside of the definition. Such definitions can use any other symbol, and still generate the same result. On the other hand, definitions established using Set (such as functionB1) rely on the result of evaluating the right-hand side containing the same symbol as the formal argument. This can be a source of subtle bugs as one must take care not use symbols that accidentally have pre-existing down-values. The use of formal symbols (described in Letters and Letter-like Forms) for argument names can help manage this problem.
You can understand what is going on by trying:
Table[functionB2[Sqrt[y]],{y,0.5,.5,.5}]
Table[functionB2[Sqrt[x]],{x,0.5,.5,.5}]
(*
{4/Sqrt[3] - 0.335/(0.435+ x)^2 + 0.347/(0.712+ x)^4 - 0.27/(4.29+ x)}
{2.03065}
*)
What is getting replaced is the x inside the definition of functionB2, not a formal argument.
Edit
The plot you are getting is not what you want. The Sqrt[x] is disregarded in functionB2[...] and the implicit x is replaced, as you can see here:

Evaluate all possible interpretations in OCaml

I need to evaluate whether two formulas are equivalent or not. Here, I use a simple definition of formula, which is a prefix formula.
For example, And(Atom("b"), True) means b and true, while And(Atom("b"), Or(Atom("c"), Not(Atom("c")))) means (b and (c or not c))
My idea is simple, get all atoms, apply every combination (for my cases, I will have 4 combination, which are true-true, true-false, false-true, and false-false). The thing is, I don't know how to create these combinations.
For now, I have known how to get all involving atoms, so in case of there are 5 atoms, I should create 32 combinations. How to do it in OCaml?
Ok, so what you need is a function combinations n that will produce all the booleans combinations of length n; let's represent them as lists of lists of booleans (i.e. a single assignment of variables will be a list of booleans). Then this function would do the job:
let rec combinations = function
| 0 -> [[]]
| n ->
let rest = combinations (n - 1) in
let comb_f = List.map (fun l -> false::l) rest in
let comb_t = List.map (fun l -> true::l) rest in
comb_t # comb_f
There is only one empty combination of length 0 and for n > 0 we produce combinations of n-1 and prefix them with false and with true to produce all possible combinations of length n.
You could write a function to print such combinations, let's say:
let rec combinations_to_string = function
| [] -> ""
| x::xs ->
let rec bools_to_str = function
| [] -> ""
| b::bs -> Printf.sprintf "%s%s" (if b then "T" else "F") (bools_to_str bs)
in
Printf.sprintf "[%s]%s" (bools_to_str x) (combinations_to_string xs)
and then test it all with:
let _ =
let n = int_of_string Sys.argv.(1) in
let combs = combinations n in
Printf.eprintf "combinations(%d) = %s\n" n (combinations_to_string combs)
to get:
> ./combinations 3
combinations(3) = [TTT][TTF][TFT][TFF][FTT][FTF][FFT][FFF]
If you think of a list of booleans as a list of bits of fixed length, there is a very simple solution: Count!
If you want to have all combinations of 4 booleans, count from 0 to 15 (2^4 - 1) -- then interpret each bit as one of the booleans. For simplicity I'll use a for-loop, but you can also do it with a recursion:
let size = 4 in
(* '1 lsl size' computes 2^size *)
for i = 0 to (1 lsl size) - 1 do
(* from: is the least significant bit '1'? *)
let b0 = 1 = ((i / 1) mod 2) in
let b1 = 1 = ((i / 2) mod 2) in
let b2 = 1 = ((i / 4) mod 2) in
(* to: is the most significant bit '1'? *)
let b3 = 1 = ((i / 8) mod 2) in
(* do your thing *)
compute b0 b1 b2 b3
done
Of course you can make the body of the loop more general so that it e.g. creates a list/array of booleans depending on the size given above etc.;
The point is that you can solve this problem by enumerating all values you are searching for. If this is the case, compute all integers up to your problem size. Write a function that generates a value of your original problem from an integer. Put it all together.
This method has the advantage that you do not need to first create all combinations, before starting your computation. For large problems this might well save you. For rather small size=16 you will already need 65535 * sizeof(type) memory -- and this is growing exponentially with the size! The above solution will require only a constant amount of memory of sizeof(type).
And for science's sake: Your problem is NP-complete, so if you want the exact solution, it will take exponential time.

Resources