2

I want to define a constant vector {$k_x , k_y , k_z$}. The problem is that Mathematica gives me D[$k_x$,x]$=k_1$. I want this to be zero, because $k_x$ is a constant.

Clearly D[$k_x$,x]$=k_1$ makes sense since $k_x=$Subscript[k,x] but it is still quite annoying because it is contrary to standard notation.

How to tell Mathematica that Subscript[k,x] is just a variable name (instead of two variables, k and x, inside the function Subscript)?

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
Quillo
  • 143
  • 5

1 Answers1

3

The simplest solution is to not use symbols inside of your subscripts:

D[Subscript["k", "x"], x]

0

In StandardForm, they look the same:

{Subscript[k, x], Subscript["k", "x"]}

enter image description here

Addendum

If you must use symbols instead of strings, then you can change a system option so that Subscript objects are not differentiable:

SetSystemOptions[
    "DifferentiationOptions" -> "ExcludedFunctions" -> DeleteDuplicates @ Append[
        OptionValue[SystemOptions[], "DifferentiationOptions"->"ExcludedFunctions"],
        Subscript
    ]
];

Then:

D[Subscript[k, x], x] //InputForm

D[Subscript[k, x], x]

Mathematica knows there's an x inside the subscript, so it thinks the result isn't 0, but the "ExcludedFunctions" setting means Mathematica doesn't know what to do with the subscript. So, we need to define an UpValues:

D[_Subscript, __] ^:= 0

Now:

D[Subscript[k, x], x]

0

Carl Woll
  • 130,679
  • 6
  • 243
  • 355