Double arguments and place of the underscore

Could someone explain me of this bit of code below is correct:

Is it possible to define a function with a second bracket, see below, i.e. f[x_][y_]? And what does this implement?
What does the syntax [C_s] do?

i[a_,b_][C_s]:=ReplacePart[C,{a->C[[b]],b->C[[a]]}]

Edit

So let me a bit more detailed:

This is the code I want to understand, and that has to implement the function
S1,2=ih(x)P1,2+I1,2, S_{1,2} = \frac{i}{h(x)}\mathbb{P}_{1,2}+\mathbb{I}_{1,2}\,,
where P1,2\mathbb{P}_{1,2} is the permutation operator and I1,2\mathbb{I}_{1,2} is the identity operator, acting on the state 1 and state 2, of a three particle state defined by:

probe=s[{a1,x1},{a2,x2},{a3,x3}];

The code is the following:

i[a_,b_][C_s]:=ReplacePart[C,{a->C[[b]],b->C[[a]]}]
p[a_,b_][C_s]:=ReplacePart[C,{{a,2}->C[[b,2]],{b,2}->C[[a,2]]}]
S[a_,b_][A]:=A/.Cs:>Block[{xa=C[[a,2]],xb=C[[b,2]]},(I/h[xa-xb])p[a,b][C]+i[a,b][C]]

I do not understand how the last line can work; what is de `Cs’ referring to?

=================

  

 

Yes, it is possible, although you may want to use the format carefully. A possible duplicate: How can I overload a function with multiple bracket-slots. See also: (5686494 on StackOverflow), (9741), (544).
– MarcoB
Feb 8 at 15:39

  

 

In this particular example, I think the s after the C_ is not valid. You could have C_List and then only arguments of type List would work there. But I don’t know of a s type
– JasonB
Feb 8 at 15:41

2

 

@Jason It should be clarified that it’s only invalid (or, more accurately, not useful) by default — if the user has implemented, or intends to implement/use the head s, then it’s perfectly valid to use C_s in a pattern. It means a single argument with head (‘type’) s.
– jjc385
Feb 8 at 15:52

  

 

Is it possible you meant for C_s to actually read C_S? As written, it will match to s[…] but not S[…].
– jjc385
Feb 8 at 16:57

1

 

Don’t use C – it’s a reserved symbol.
– Jens
Feb 8 at 18:14

=================

1 Answer
1

=================

First question: See How can I overload a function with multiple bracket-slots so f[a][b] and f[a] can coexist?, and the comments.

Second question: Without the s, C_ is a pattern which we can call C on the right hand side. (It seems like you understand this.)

With the s included, C_s is likewise a pattern that we can call C, but it only matches expressions with head s. (Head might be thought of like ‘type’ in other languages.) You can test the head of any expressions with

Head/@{ f[x], f[x][y], {x}, x+y, x*y, x }

{f, f[x], List, Plus, Times, Symbol}

The quick answer is that C_s will only match an expression that looks like s[ … ]. Thus, you get something like this:

f[x_][C_s] := foo[ x, C ]

f[x][ y ]

f[x][y]

f[x][ s[y] ]

foo[ x, s[y] ]

Note that C on the right hand side includes both the symbol s and its arguments.

Be careful of

f[x][ s ]

f[x][s]

This is because Head[s] evaluates to Symbol. This is true even if you have definitions for things like s[x_], as long as s itself hasn’t been given definitions with = or :=.

Edit

(I initially misread the question, and thought ‘Cs’ referred to the individual C’s and s’s, not the symbol Cs.)

Addressing the last line of the question, Cs is not defined anywhere in the code you’ve given. As written, the right hand side of the last line will replace literal occurrences of Cs with Block[…].

However, since the left hand side of the := contains A and not A_, the only possible value of A on the right hand side is the literal symbol A. (It is very likely this is a mistake.)