Mathematica: Remove argument from flat operator - wolfram-mathematica

I have an object in mathematica with the attributes Flat and OneIdentity. I also defined the following substitutions:
A[z___, x_Times /; ! FreeQ[x, b], y___] :=
A[z, A## x, y]
A[z___, x_Plus /; ! FreeQ[x, b], y___] :=
A[z, #, y] & /# (x)
I need them so that all products inside A become part of the arguments of A, and the distributive property with respect to the sum holds.
I need to find a way to remove all the arguments that do not contain b pattern.
I tried with A[z___, a_?(FreeQ[#, b] &), y___] := a A[z, y] , but I get IterationLimit error.
I understand that the reason of the error is that mathematica will match A[a] as a with flat operators. But then what is the correct way to do that, in such a way that is compatible with the previous two conditions?

Related

Make multiple replacements in a list by index

I have this program that makes multiple replacements in a list given by
another list with pairs in the format (Index, Elem).
Example:
replace_multiple([A, B, C, D, E], [(2, b), (1, a), (3, c), (4, d)]).
###
should result:
####
Lst = [a, b, c, d, E],
false
####
However this is my output:
A = a,
B = b,
C = c,
D = d
false
####
What am i doing wrong?
What am i doing wrong?
Here are a few things:
You may be unsure about the fact that the list you want to replace contains elements starting with uppercase letters, i.e. logical variables.
In replace_multiple/2, the new element is described as Elem in the head, but as Letter in the body.
In replace_multiple/2, the call to replace/4 has Lst on first and second position. The meaning of a logical variable like Lst is the same as the name of a shared global variable, which, however, cannot be modified unless you replace more logical variables in that logical variable with other stuff. So you have the same structure as "input" and "output". This is probably not what you want.
Same for replace_multiple/2 per se, which should take Lst and build ("return") a modified LstMod given as third argument.
There is fat syntax error at the end of replace/4.
It is better to use [x,y] or x-y than (x,y) as a notation for a pair.

SymbolName applied to a list of variables, some of which may have values assigned

In Mathematica:
I would like to pass a variable number of arguments to a function.
I would like to print the name of each argument. The problem is that SymbolName evaluates its input. For a given variable, you can get around this:
a=18;
SymbolName[Unevaluated[a]]
works. But that won't work if the variable is in a list. For example:
printname[x__]:=Print[Table[SymbolName[Unevaluated[{x}[[i]]]],{i,1,Length[{x}]}]];
printname[a,b,c]
will not work. Any suggestions?
Thanks in advance.
Mathematica tries to evaluate the argument of Unevaluated[] when you call it. So {x}[[i]] gets converted into {18, b, c}[[i]] which you didn't want and then the iteration over i doesn't work anymore because Unevaluated[] doesn't let the Table access the iterator.
So, to really solve the issue you should disable Mathematica's evaluation completely for the functions that you want to pass the symbols through.
In[1]:= SetAttributes[SymbolName, HoldAll];
SetAttributes[Map, HoldAll];
After this you can just do
In[2]:= a=18; SymbolName ### Unevaluated /# {a, b, c}
Out[2]:= {a, b, c}
where ### and /# are shorthand for Apply[] and Map[].
Setting Hold[] or similar attributes in Mathematica's built in functions can lead to trouble. See this question and answer in the Mathematica stackexchange for more information.
Specifically, to make a function that takes an arbitrary number of arguments would be
sym = SymbolName ### Unevaluated /# {##} &
But the List[] function that takes the sequence of arguments ## for the function & will again evaluate a and turning HoldAll on for List[] is not OK.
Thus the easiest way to do this is to define a function with HoldAll that just passes the args into a Block[] as the list of local variables. This makes a creates an isolated context where the variables do not evaluate to anything.
In[1]:= SetAttributes[f, HoldFirst];
In[2]:= f[seq__] := Block[{seq}, Print[SymbolName /# {seq}]];
In[3]:= a=18; f[a, b, c]
Out[3]:= {a, b, c}

Mathematica, nested optimization

I want to use the solution of Maximization, defined as a function, in another function. Here's an example:
f1[y_] := x /. Last[Maximize[{Sin[x y], Abs[x] <= y}, x]] (* or any other function *)
This definition is fine, for example if I give f1[4], I get answer -((3 \[Pi])/8).
The problem is that when I want to use it in another function I get error. For example:
FindRoot[f1[y] == Pi/4, {y, 1}]
Gives me the following error:
ReplaceAll::reps: {x} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>
FindRoot::nlnum: The function value {-0.785398+(x/.x)} is not a list of numbers with dimensions {1} at {y} = {1.}. >>
I've been struggling with this for several days now! Any comment, idea, help, ... is deeply appreciated! Thank you very much!
When y is not a number, your Maximize cannot be resolved, in which case the Last element of it is x, which is why you get that odd error message. You can resolve this by clearing the bad definition of f1 and making a new one that ensures only numeric arguments are evaluated:
ClearAll[f1]
f1[y_?NumericQ] := x /. Last[Maximize[{Sin[x y], Abs[x] <= y}, x]]
FindRoot[f1[y] == \[Pi]/4, {y, 1}]
(* {y -> 0.785398} *)

A problem when wrongly using Dot command in Mma

In[1]:= SameQ[Dot[1, 2], 1.2]
TrueQ[Dot[1, 2] == 1.2]
a = 1; b = 2;
SameQ[Dot[a, b], a.b]
TrueQ[Dot[a, b] == a.b]
Out[1]= False
Out[2]= False
Out[4]= True
Out[5]= True
I know this uses Dot command wrong. Anybody can give me a clear reson for the above different results?
thanks!
a.b is interpreted as Dot[a,b] and then variables a and b are substituted, meaning Dot[1,2] and thus equality holds. This is not the same as 1.2 where the dot stands for the decimal separator and not for the inline operator of Dot.
When you write 1.2, Mma understands a number (aka 6/5), but if you write {1, 1}.{2, 2} or a.b Mma understands a scalar product, as usual in any book using vectors.
HTH!
It can be informative to view an expression under Hold and FullForm:
a = 1; b = 2;
SameQ[Dot[a, b], a.b]] //Hold //FullForm
Hold[SameQ[Dot[a, b], Dot[a, b]]]
With this combination of commands, Mathematica parses but does not evaluate the expression (Hold), and then shows the long pseudo-internal form of the expression (FullForm).
In this case, you can see that the second term a.b is parsed as Dot[a, b] before any evaluation happens.
When . appears with numerals as in 1.2 it is interpreted specially as a decimal point. This is similar to other numeric entry formats such as: 1*^6 which is recognized directly as 1000000:
1*^6 //Hold //FullForm
Compare trying to enter:
a = 1;
a*^6

In Mathematica, what does ### mean?

I've been working through problems on Project Euler, and some of the solutions that other people have posted use a triple-at-sign, i.e. '###'. In the help browser for v7, I find an entry for ## (which says it's the infix version of 'Apply') but none for ###. What does it mean?
EDIT: Here's an example, which I think I can post without violating the spirit of Project Euler:
bloc[n_, f_][t_] := {f ### #, #~Tr~f} & /# Join ## Partition[t, {n, n}, 1];
As others have noted, ### is, technically, shorthand for Apply with an optional third argument, as is explained deep in the documentation for Apply.
But I like to think of
f ### {{a,b}, {c,d}, {e,i}}
as shorthand for
f ###& /# {{a,b} {c,d}, {e,i}}
In other words, take a pure function (shorthand: ...#...&) that does an Apply (shorthand: ##) to a list of arguments, and Map (shorthand: /#) that over a list of such lists of arguments.
The result is
{f[a,b], f[c,d], f[e,i]}
### is the short form for Apply at level 1.
f ### {{a, b, c}, {d, e}}
is equivalent to
Apply[f, {{a, b, c}, {d, e}}, {1}]
Reference: http://reference.wolfram.com/mathematica/ref/Apply.html
You may need to expand the Scope and Level Specification sections.
f ### expr is equivalent to Apply[f, expr, {1}].
documents.wolfram.com

Resources