2
$\begingroup$

The equivalent resistance $ R_{eq}$ of $n$ resistors in parallel can be calculated from this:
enter image description here

Would it be possible to define a function to calculate the equivalent resistance of resistors with the function name ||? I would like to apply it like this: R1 || R2, R1 || R2 || R3, or any number of parameters, and it would return the equivalent resistance.
I know how to write a standard function to calculate the equivalent resistance of parallel resistors. However, what I am seeking here is the ability to use a function named || and apply it in the manner of R1 || R2, R1 || R2 || R3, as we typically write.
Here are typical functions for calculating equivalent parallel resistance.

$\endgroup$
10
  • $\begingroup$ Be aware that you will have to tell the operator precedence (for example should : R1+R2 || R3 be interpeted as (R1+R2) || R3 or R1+(R2 || R3) ? ), unless you use all the necessary parenthesis systematically. $\endgroup$
    – andre314
    Apr 6 at 21:05
  • $\begingroup$ @andre314 Users should add parenthesis to make it behave as they want. Thanks for clarification. $\endgroup$
    – internet
    Apr 6 at 21:06
  • 1
    $\begingroup$ || already has a definition as the built-in function Or. $\endgroup$
    – Chris K
    Apr 6 at 21:12
  • 1
    $\begingroup$ This answer has a nice list of potential candidates $\endgroup$
    – Lukas Lang
    Apr 6 at 21:24
  • 1
    $\begingroup$ If you really insist on using ||, you can do this by creating an auxilliary function: eval[e_] := e /. Or -> (1/Total[1/{##}] &); R1 || R2 || R3 // eval Alternatively, you could put this into $Post if you really do not expect to use the logical Or for its regular purposes. $\endgroup$
    – Domen
    Apr 6 at 21:43

1 Answer 1

8
$\begingroup$

It won't work with || for reasons described in the comments. However, you can use a unicode character or U+2225 which Mathematica just so happens to call \[DoubleVerticalBar]. Then you can define some infix notation for it:

Remove["Global`*"];
Needs["Notation`"];
RParallel[x__] := 1/(Total[1/{x}])
AddInputAlias["4" -> ParsedBoxWrapper["∥"]];
InfixNotation[ParsedBoxWrapper["∥"], RParallel];

a ∥ b ∥ c ∥ d

(* 1/(1/a + 1/b + 1/c + 1/d) *)
$\endgroup$
4
  • $\begingroup$ Thanks. It's a very nice solution. I just realized that using // (Postfix) is even more convenient to type, so I'm trying to make that work as well while keeping the built-in function. $\endgroup$
    – internet
    Apr 7 at 5:35
  • $\begingroup$ Would there be a way to shorten the typing of \[DoubleVerticalBar] as I use it a lot? $\endgroup$
    – internet
    Apr 7 at 14:00
  • 1
    $\begingroup$ @internet no, however, you can just type \[Dou and find it in the autocomplete from the notebook interface, or copy and paste the symbol. $\endgroup$
    – flinty
    Apr 11 at 19:15
  • 1
    $\begingroup$ @internet, see InputAliases, e.g. CurrentValue[$FrontEndSession, {InputAliases, "xx"}] = "\[DoubleVerticalBar]" $\endgroup$
    – Domen
    Apr 11 at 20:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.