My mathematica output is
-0.751988 - 0.0708732 Log[e] - 0.0140273 Log[e]^2
But I want mathematica to calculate this expression for me i.e. set Log[e] = 1 and sum the terms. How do I instruct it to do that. I'm assuming it must be treating the functions as complex??
Regards
You probably wanted E or \[ExponentialE] which has the input alias ⋮ee⋮.
Built-in symbols are capitalized, so constants like pi, e are written as Pi, E
If i remember correctly, Mathematica use E as the Neper number, not e. I think you mispelled it, so Log[e] is not expanded.
If not so, with the substitution Log[e]->1 you can achieve what you want.
Related
How to write something like this expression on WolframAlpha to get the correct answer?
Solve[Sort[IntegerDigits[2*x]] == Sort[IntegerDigits[3*x]], x]
The first possible x value equals to 1782.
2*1782 = 3564
3*1782 = 5364
Which are the same after sorting.
Sometimes it is possible to know enough Mathematica language notation that this can be used to coax WolframAlpha into giving an answer. This
Select[Table[{x, Sort[IntegerDigits[2*x]],Sort[IntegerDigits[3*x]]}, {x,6000}], #[[2]]==#[[3]]&]
or this
Select[Table[x,{x,6000}],Sort[IntegerDigits[2*#]]==Sort[IntegerDigits[3*#]]&]
works
I need (for design choices) to obtain a list who respects the following pattern:
Uses = ['foo\/1', 'foobar\/2'].
I'm able to build up the
name/number pattern
doing:
all((P\/A), (rule(X, Ux, _, Module), member(U, Ux), U = (P/A)), Uses).
where rule is an internal fact and Ux is a list.
I can escape slashes easily, using the '/' shortcut, but what about putting (P/A) between quotes?
How do that? please help me.
If you want to obtain 'foo/1', you can easily use atomic_list_concat/2 predicate as follows:
Functor=foo,
Arity=1,
atomic_list_concat([Functor, '/', Arity], Output).
In this way Output variable will be bound to 'foo/1' term.
Just put them between 3 apexes:
?- A=foo, B=1, writeln('''A/B''').
'A/B'
A = foo,
B = 1
I would like to define an object/symbol in mathematica which will have several parameters, for example, something like: S=(1-t)({b_i}^x,{b_i}^y)+t({b_{i+1}}^x,{b_{i+1}}^y) (sort of LaTeX notation). In the example I'm trying to describe the line segment connecting two points b_i and b_{i+1}.
How can I define such an object in mathematica?
I found the following two questions:
Mathematica Notation and syntax mods
Subscripted variables
But I'm not sure that I am using them correctly. What I have done is the following. First I invoked:
Needs["Notation`"];
Symbolize[
ParsedBoxWrapper[
SubscriptBox["\[SelectionPlaceholder]", "\[Placeholder]"]]]
Symbolize[
ParsedBoxWrapper[
SuperscriptBox["\[SelectionPlaceholder]", "\[Placeholder]"]]]
Then, I actually defined the object:
(1 - t) {Subscript[b, i]^x, Subscript[b, i]^y} +
t {Subscript[b, i + 1]^x, Subscript[b, i + 1]^y}
Is this the right way to do what I want?
I'm not entirely sure I understand what you want. The 'object' you are talking about
(1 - t) {Subscript[b, i]^x, Subscript[b, i]^y} +
t {Subscript[b, i + 1]^x, Subscript[b, i + 1]^y}
isn't really a single entity, but the sum of two lists each consisting of two components. So, I assume that you really want to define Subscript[b, i]^x as a symbol.
You can do this with Symbolize from the Notation package. It's absolutely crucial, though, that you use the template generated when you press the Symbolize button on the Notation palette (you get this upon running << Notation`). Then enter your compound variable. I'll be assuming the superscript x and y are fixed symbols and the subscript is are variable:
One more thing:
It might not be a good idea to use Subscript[b, i]^y as you'd lose the ability to raise subscripted variables to the power x and y (a minor loss, but still). Instead you might want to use Subsuperscript[b,i,y]. Please note that the sentence in the 'More information' part of the Subsuperscript documentation page seems to be patently wrong. It says:
To enter a subsuperscript in a notebook, use either Ctrl+_ to begin a
regular subscript or Ctrl+^ to begin a regular superscript. After
typing the first script, use Ctrl+% to move to the opposite script
position. Ctrl+Space moves out of the subscript or superscript
position.
If you do a FullForm on the resulting object you'll see you have made a Subscript[b, i]^y instead. To get the symbol for pasting in the Symbolize template I see no other solution than typing Subsuperscript[b, i_, y], evaluating, and copying the result to the template.
Further to Sjoerd's answer: Since you say the symbol S takes various parameters, I think you might want to explore theSetDelayed method to define a function with parameters. Assumuing that you do want S to be vector-valued with two points, then something like the following would define S in the way you want it.
S[x_,y_,t_,i_]:= (1-t) * {b[i]^x,b[i]^y} + t * {(b[i+1])^x,(b[i+1])^y}
The question then is whether you really need the subscripting. Sjoerd's answer shows how this is done using the Notation package, but you should consider whether this additional complication is necessary to your analysis.
EDIT in response to rcollyer's very helpful comment
You can use Format to define a TraditionalForm representations for a custom function. This is similar to defining UpValues, but relates to the representation rather than the re-writing rules.
Something like the following should work:
Format[S[x_,y_,t_,i_],TraditionalForm] :=
(1 - t) {Subscript[b, i]^x, Subscript[b, i]^y} +
t {Subscript[b, i + 1]^x, Subscript[b, i + 1]^y}
In Mathematica, how do you change the order of importance of variables? for example: if i enter b+c+a+d, i get a+b+c+d but i want b and d to preceed other variables. so that i get b+d+a+c
note, i'd like to use it where + is non-commutative
First you need to define an ordering function like:
In[1]:= CPOrdering[a]=3;
CPOrdering[b]=1;
CPOrdering[d]=2;
CPOrdering[c]=4;
Although, for more complicated examples, you should probably be smarter about it than this - ie use pattern matching.
Then you can sort expressions using
In[5]:= CirclePlus[a,b,c,d]
SortBy[%,CPOrdering]
Out[5]= a\[CirclePlus]b\[CirclePlus]c\[CirclePlus]d
Out[6]= b\[CirclePlus]d\[CirclePlus]a\[CirclePlus]c
This can then be automated using something like
CPOrdering[a_, b_] := CPOrdering[a] < CPOrdering[b]
CirclePlus[a__] /; (!OrderedQ[{a}, CPOrdering]) := CirclePlus##SortBy[{a}, CPOrdering]
The underlying reason b+c+a+d becomes a+b+c+d in Mathematica is because Plus has the attribute Orderless. In general, a symbol f with attribute Orderless means that the elements of f in an expession f[e1, e2, e3], the elements ei should be sorted into canonical order, and in particular, Mathematica's canonical order equivalent to that of OrderedQ and Ordering.
Orderless is even accounted for during pattern matching:
In[47]:= a+b+c+d /. a+c -> e
Out[47]= b+d+e
It's highly, highly recommended that you do NOT remove the Orderless attribute from Plus because the consequences could be dire for lots of functionality in Mathematica.
As other posters have noted, your best bet is to simply define your own function that is NOT Orderless, and will therefore preserve argument order. You might also find HoldForm useful in very limited circumstances.
Also note that nothing stops you from typesetting symbols in an expression in whatever order you want in a notebook, as long as you don't evaluate-in-place, etc.
So, don't use "+", because Plus[] IS commutative.
Define your own myPlus[x_,y_]:= .... whatever.
If you have an idea of what your new Plus[] should do, post it and we may try to help you with the definition/
HTH!
PS> You may change the definition of Plus[] ... but :)
I don't know why Wikipedia lists Mathematica as a programming language with printf. I just couldn't find the equivalent in Mathematica.
My specific task is to process a list of data files with padded numbers, which I used to do it in bash with
fn=$(printf "filename_%05d" $n)
The closest function I found in Mathematica is PaddedForm. And after some trial and error, I got it with
"filename_" <> PaddedForm[ Round##, 4, NumberPadding -> {"0", ""} ]&
It is very odd that I have to use the number 4 to get the result similar to what I get from "%05d". I don't understand this behavior at all. Can someone explain it to me?
And is it the best way to achieve what I used to in bash?
I wouldn't use PaddedForm for this. In fact, I'm not sure that PaddedForm is good for much of anything. Instead, I'd use good old ToString, Characters and PadLeft, like so:
toFixedWidth[n_Integer, width_Integer] :=
StringJoin[PadLeft[Characters[ToString[n]], width, "0"]]
Then you can use StringForm and ToString to make your file name:
toNumberedFileName[n_Integer] :=
ToString#StringForm["filename_``", toFixedWidth[n, 5]]
Mathematica is not well-suited to this kind of string munging.
EDIT to add: Mathematica proper doesn't have the required functionality, but the java.lang.String class has the static method format() which takes printf-style arguments. You can call out to it using Mathematica's JLink functionality pretty easily. The performance won't be very good, but for many use cases you just won't care that much:
Needs["JLink`"];
LoadJavaClass["java.lang.String"];
LoadJavaClass["java.util.Locale"];
sprintf[fmt_, args___] :=
String`format[Locale`ENGLISH,fmt,
MakeJavaObject /#
Replace[{args},
{x_?NumericQ :> N#x,
x : (_Real | _Integer | True |
False | _String | _?JavaObjectQ) :> x,
x_ :> MakeJavaExpr[x]},
{1}]]
You need to do a little more work, because JLink is a bit dumb about Java functions with a variable number of arguments. The format() method takes a format string and an array of Java Objects, and Mathematica won't do the conversion automatically, which is what the MakeJavaObject is there for.
I've run into the same problem quite a bit, and decided to code my own function. I didn't do it in Java but instead just used string operations in Mathematica. It turned out quite lengthy, since I actually also needed %f functionality, but it works, and now I have it as a package that I can use at any time. Here's a link to the GitHub project:
https://github.com/vlsd/MathPrintF
It comes with installation instructions (really just copying the directory somewhere in the $Path).
Hope this will be helpful to at least some.
You could also define a function which passes all arguments to StringForm[] and use IntegerString or the padding functions as previously mentioned:
Sprintf[args__] := StringForm[args__] // ToString;
file = Sprintf["filename_``", IntegerString[n, 10, 5]];
IntegerString does exactly what you need. In this case it would be
IntegerString[x,10,5]
I agree with Pillsy.
Here's how I would do it.
Note the handy cat function, which I think of as kind of like sprintf (minus the placeholders like StringForm provides) in that it works like Print (you can print any concatenation of expressions without converting to String) but generates a string instead of sending to stdout.
cat = StringJoin##(ToString/#{##})&;
pad[x_, n_] := If[StringLength#cat[x]>=n, cat[x],
cat##PadLeft[Characters#cat[x],n,"0"]]
cat["filename_", pad[#, 5]]&
This is very much like Pillsy's answer but I think cat makes it a little cleaner.
Also, I think it's safer to have that conditional in the pad function -- better to have the padding wrong than the number wrong.