set-fontset-font only overrides some Unicode character ranges (OS X) - macos

If I put the following into a scratch buffer (running emacs -q on 24.4.1), I can get the symbols (those in the comment) to change by varying the font name (changing the size so that it becomes "MS PGothic-24" for example). This is nice, and seems to show set-fontset-font working as intended.
; multiset symbols (unions with stuff inside them: ⊌ ⊎
(set-fontset-font t '(#x228C . #x228E) "MS PGothic-22")
However, if I have
; greek alphabet: α β γ δ
(set-fontset-font t '(#x370 . #x3F0) "MS PGothic-22")
then I seem to be trapped in a Menlo font. If I put cursor on the character and do C-u C-x =, I get output including the following:
mac-ct:-*-Menlo-normal-normal-normal-*-12-*-*-*-m-0-iso10646-1 (#x2F9)
The rest of the output confirms that the character I'm looking at is in the given range:
character: α (displayed as α) (codepoint 945, #o1661, #x3b1)
By contrast, the output for the union-symbols is
mac-ct:-*-MS PGothic-normal-normal-normal-*-22-*-*-*-p-0-iso10646-1 (#x43A0)

Related

What does an 'at' # sign mean in Julia?

For example, here:
ys = lift(frequency, phase) do fr, ph
#. 0.3 * sin(fr * xs - ph)
end
from here.
I am failing to interpret it as a macro definition or call.
TLDR: # invokes a macro. https://docs.julialang.org/en/v1/manual/metaprogramming/
One of julia's best features in my opinion are it's macros. They allow you to easily write functions that manipulate source code. #. for example turns 0.3 * sin(fr * xs - ph) into 0.3 .* sin(fr .* xs - ph). Another common example is #time which roughly would translate the same expression to
t1=time()
0.3 * sin(fr * xs - ph)
println(time()-t1)
Note that neither of these are achievable by a function, since functions have their inputs evaluated before they run, while macros instead operate on the code itself.
I think it is worth to add that it is easy to learn what #. does by invoking help. Press ? then write #. and hit enter to get:
help?> #.
#. expr
Convert every function call or operator in expr into a "dot call" (e.g.
convert f(x) to f.(x)), and convert every assignment in expr to a "dot
assignment" (e.g. convert += to .+=).
If you want to avoid adding dots for selected function calls in expr, splice
those function calls in with $. For example, #. sqrt(abs($sort(x))) is
equivalent to sqrt.(abs.(sort(x))) (no dot for sort).
(#. is equivalent to a call to #__dot__.)
Examples
≡≡≡≡≡≡≡≡≡≡
julia> x = 1.0:3.0; y = similar(x);
julia> #. y = x + 3 * sin(x)
3-element Array{Float64,1}:
3.5244129544236893
4.727892280477045
3.4233600241796016
where you also learn that #. is just a shorthand for #__dot__.
And if you want to find its definition write e.g.:
julia> #which #. 1
#__dot__(__source__::LineNumberNode, __module__::Module, x) in Base.Broadcast at broadcast.jl:1241
to get the exact information on location of its implementation or write #edit #. 1 and it will get opened-up in your editor.
Above I have commented on how to learn what #. does and how it is implemented. If you want to learn what is the effect of #. (or any macro in general) you can use the #macroexpand macro. Therefore if you write e.g.:
julia> #macroexpand #. coalesce(sin(#view x[:, 1]), 0.0)
:(coalesce.(sin.(true && (view)(x, :, 1)), 0.0))
you can see how #. macro rewrites your original expression coalesce(sin(x), 0.0). Note that in this case the expressions are not evaluated - you only get an equivalent expression with all macros removed.
If you wanted to see only #. macro expanded (assuming - as in an example above it is the outermost macro) then use:
julia> #macroexpand1 #. coalesce(sin(#view x[:, 1]), 0.0)
:(coalesce.(sin.(#= REPL[11]:1 =# #view(x[:, 1])), 0.0))
As you can see #macroexpand1 is not recursive and only expanded the outermost macro and left #view macro untouched.

How to remove multiple consecutive white space in Prolog using only get0 predicate

I have a program that read a string from the input and removes the multiples blank spaces between words changing them into single withe space using both get0 and get predicate:
squeeze :- get0(C),
put(C),
dorest(C).
dorest(46).
dorest(32) :- !,
get(C),
put(C),
dorest(C).
dorest(Letter) :- squeeze.
This is pretty simple, now I have an exercise that ask mw to create a new version of the previous program that use only get0 built in predicate
I am finding some difficulties with this version.
This is my personal solution of the problem (that don't work well):
squeeze2 :- NumBlank is 0, % At the beginning of a word the number of blank character is 0
get0(Char), % Read a character that could be a blank
%put(Char),
dorest2(Char, NumBlank).
dorest2(46, _) :- !. % If the read character is a full stop character the program end
% Read a white space but the number of white space is 1
dorest2(32, NumBlank) :- !,
NumBlankUpdated is NumBlank + 1, % Update the number of blanks
NumBlankUpdated < 2, % The number of blank have to be at most 1
put(32), % Print a white space
get0(Char), % Read a character
dorest2(Char, NumBlankUpdated). % Call dorest2
% Read a white space but the number of white space is > 1:
dorest2(32, NumBlank) :- !,
NumBlankUpdated is NumBlank + 1, % Update the number of blanks
NumBlankUpdated >= 2, % The number of blanks is >1
get0(Char), % Read a character and don't print anything
dorest2(Char, NumBlankUpdated). % Call dorest2
% Id the read character it is a letter (not a blank) print it
dorest2(Letter2, NumBlank) :- !,
put(Letter2),
squeeze2. % Read an other character that could be a blank
My idea to solve it using only the get0 predicate involves count the number of white spaces, and based on that value do different things
The squeeze2 predicate is called when a new word begin so the number of consecutive withe spaces found it is 0. It read a character from the input and call the dorest2/2 predicate.
Now I have divided into 4 differents cases the dorest2/2 predicate and using the cut operator these cases are mutually exclusive (like a procedural if):
1) The FIRST CASE it is related at the read of a full stop character (the '.' character) that corresponds at the end of the program.
2) The SECOND CASE it is related at the read of the first blank character between 2 words so this single white space have to be print by put predicate. In this case there is an update of the counter that count the number of the sequentual white characters.
Then, another character is read.
3) The: THIRD CASE it is realated to the situaion in which the program read a second consecutive white character, in this case this withe character it is not print and another character is read and the white character counter is updated with the new number of sequential white characters found.
4) The FOURTH CASE it is related to the situaion in which the program read a character that is not a white space or a full stop character so this character have to be a letter and this means that a new word is beginning. So simply have to print this letter (by the put) and call the squeeze2 predicate that reset to 0 the sequential white character counter and read a new character
The problem is that in the multiple consecutive blanks characters don't work.
If I perform a query like these work well:
STRING WITHOUT NOT BLANK CHARACTER:
[debug] [2] ?- squeeze2.
|: ciao.
ciao
true.
This work well.
STRING THAT CONTAINS ONLY SINGLE WHITE CHARACTERS BETWEEN WORDS:
[debug] [2] ?- squeeze2.
|: ciao.
ciao
true.
This also work well
But in this situation I have an error:
STRING THAT CONTAINS MULTIPLE WHITHE CHARACTWERS BETWEEN WORDS:
[debug] [2] ?- squeeze2.
| multiple blanks characters.
multiple
false.
ERROR: Syntax error: Operator expected
ERROR: blanks
ERROR: ** here **
ERROR: characters .
It seems that the problem is in the THIRD CASE but I am not understanding where is the error because this case seems to me very simple: if the counter of consecutive white characters is > 1 then don't print anything and continue to write untill a new word begin.
Where is the problem? Someone can help me?
Tnx
Andrea
Here some code working, that shows an alternative for representing character constants, instead of numeric codes:
squeeze2 :-
get0(C),
squeeze2(C, 0).
squeeze2(C, N) :-
( [C] == "."
-> true
; ( [C] == " "
-> ( N == 0
-> put(C)
; true
),
get0(D),
squeeze2(D, 1)
; put(C),
get0(D),
squeeze2(D, 0)
)
).
It boils down to a single character lookahead.
About your code: the problem it's the cut after the first space matching
dorest2(32, NumBlank) :- !, % remove this
...
after cut deletion it works:
?- squeeze2.
|: a b c .
a b c
true .

Mathematica not interpreting CenterDot as Times in numerical calculations

Why doesn't Mathematica show the numerical result of
(0.8\[CenterDot]452\[CenterDot]20+1.5\[CenterDot]4180\[CenterDot]10
-2\[CenterDot]900\[CenterDot]100) / (0.8\[CenterDot]452
+1.5\[CenterDot]4180-1\[CenterDot]2\[CenterDot]900) // N
Just to complete some of the other answers/comments, if you want CenterDot to be interpreted as Times in both input and output by using something like
Unprotect[CenterDot, Times];
CenterDot = Times;
Times /: MakeBoxes[Times[a__], fmt_] :=
With[{cbox = ToBoxes[HoldForm[CenterDot[a]]]},
InterpretationBox[cbox, Times[a]]];
Protect[CenterDot, Times];
Which you can add to your init.m if you want it loaded by default.
This works on both numeric and symbolic expressions, e.g.
In[5]:= 1\[CenterDot]2\[CenterDot]3
Out[5]= 6
In[6]:= a b c
Out[6]= a\[CenterDot]b\[CenterDot]c
You can also make the automatically inserted multiplication symbol between space separated numbers be CenterDot by executing
SetOptions[EvaluationNotebook[],
{AutoMultiplicationSymbol -> True, NumberMultiplier -> "\[CenterDot]"}]
or by selecting Center Dot in the preferences dialog under Appearance > Numbers > Multiplication.
For example:
Just replace \[CenterDot] by a space
Multiplication in Mathematica is written either as a space (Times[a,b] == a b) or as an asterisk (Times[a,b] == a*b). \[CenterDot] is not interpreted as multiplication.
I think Simon's first method can be written more concisely. Please review:
Unprotect[Times];
CenterDot = Times;
Format[a_*b__] := Interpretation[HoldForm[a\[CenterDot]b], a*b];
Second attempt. I believe this works properly with Convert To > StandardForm and editing.
CenterDot = Times;
MakeBoxes[Times[x__], _] := RowBox # Riffle[ToBoxes /# {x}, "\[CenterDot]"]

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:

What does # mean in Mathematica?

Does anyone know What # in for example Root[-1 - 2 #1 - #1^2 + 2 #1^3 + #1^4 &, 1] means in Mathematica?
Then what does Root[-1 - 2 #1 - #1^2 + 2 #1^3 + #1^4 &, 1] exactly mean?
Thanks.
It's a placeholder for a variable.
If you want to define a y(x)=x^2 function, you just could do:
f = #^2 &
The & "pumps in" the variable into the # sign. That is important for pairing & and # when you have nested functions.
In: f[2]
Out: 4
If you have a function operating on two vars, you could do:
f = #1 + #2 &
So
In: f[3,4]
Out: 7
Or you may have a function operating in a list, so:
f = #[[1]] + #[[2]] &
So:
In: f[{3,4}]
Out: 7
About Root[]
According to Mathematica help:
Root[f,k] represents the exact kth root of the polynomial equation f[x]==0 .
So, if your poly is x^2 - 1, using what we saw above:
f = #^2 - 1 &
In[4]:= Root[f, 1]
Out[4]= -1 (* as we expected ! *)
And
In[5]:= Root[f, 2]
Out[5]= 1 (* Thanks God ! *)
But if we try with a higher order polynomial:
f = -1 - 2 #1 - #1^2 + 2 #1^3 + #1^4 &
In[6]:= Root[f, 1]
Out[6]= Root[-1 - 2 #1 - #1^2 + 2 #1^3 + #1^4 &, 1]
That means Mathematica doesn't know how to caculate a symbolic result. It's just the first root of the polynomial. But it does know what is its numerical value:
In[7]:= N#Root[-1 - 2 #1 - #1^2 + 2 #1^3 + #1^4 &, 1]
Out[7]= -2.13224
So, Root[f,k] is a kind of stenographic writing for roots of polynomials with order > 3. I save you from an explanation about radicals and finding polynomial roots ... for the better, I think
How to find out what any built-in syntax means in Mathematica:
Copy expression
Do TreeForm[Hold[paste the expression here]].
Mouse-over parts of the tree to identify the syntax in question, in this case Slot
Enter "?Slot"
Notation # is (as stated above) used to mean "a variable goes here" in a pure function ("closure" for you traditional developers). It must always be followed at the end by &.
Best example is this: f[x_]:=x+5. This creates a delayed set, that any time a value is passed into a symbol reference f as a functional parameter, that value will be given a local context function-specific name of x (not affecting the global definition of x, if there is one). Then the expression x+5 will be evaluated using this new variable/value. The above process requires that symbol f be initialized, local variable x created, and expression x+5 is permanently held in memory, unless you clear it.
Side note: f=5 and f[x_]:=5 both work with a "Symbol" f. f can be referred to as a function, when square brackets are used to extract its value, and f[x_] can peacefully co-exist with f[x_,y_] without overriding each other. One will be used when one parameter is sent, and another when 2 parameters are sent.
Some times you just need a quick function and do not need to define it and leave it hanging. So, (someValue + 5) becomes (#+5)&, where & says "I'm a pure function, and will work with whatever you send me", and # says "I'm the parameter (or a parameter list) that was sent to the pure function". You can also use #1, #2, #3, etc, if you're sending it more than 1 parameter.
Example of multi-parameter pure function in common use:
Let's say mydata is a list of lists, which you need to sort by median of the lists (e.g. housing price data from various US cities):
Sort[ myData , Median[#1] > Median[#2]& ]
Quick tip, if you're applying a function to a single value, it may look neater and cleaner, and uses less typing to use # instead of [], which essentially means Prefix. Do not confuse with Map (/#) or Apply(##). The above command then becomes:
Sort[ myData , Median##1 > Median##2 & ]
You can chain # as such: Reverse#Sort#DeleteDuplicates[...]
#1 represents the first argument in a pure function.
If you have multiple arguments #1, #2, #3... refer to the first, second, third argument and so on.

Resources