3
$\begingroup$

Consider example:

    expr := {-1, 1} * value
    expr /. {value -> {10, 20}}

(* {{-10, -20}, {10, 20}} *)

however, I expect to have something like

    {-1, 1} * {10, 20}

(* {-10, 20} *)

I understand that my problems come from early expansion of expr

    expr
    (* {-value, value} *)

but I don't know, how to deny early expansion of Times operator in expr. Is there any way to do it?

$\endgroup$
  • 2
    $\begingroup$ expr := Hold[{-1, 1}*value] $\endgroup$ – Dr. belisarius Feb 24 '16 at 6:52
  • 2
    $\begingroup$ Thanks! My original case was a bit complicated to post here, so I've decided to make it small and easy to understand. It seems I have to post a new question with more context, since I don't see a way to use Hold/ReleaseHold in my setup. BTW is there any way to say, that value is vector of two elements, so that mathematica wouldn't expand it like a vector times scalar? $\endgroup$ – alvelcom Feb 24 '16 at 7:32
  • 1
    $\begingroup$ In Mathematica you'll always find one more way to do such things. "The better" way depends strongly on your use case. So yes, go on and post a more specific question. $\endgroup$ – Dr. belisarius Feb 24 '16 at 7:37
  • 1
    $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 0) Browse the common pitfalls question 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Dr. belisarius Feb 24 '16 at 7:38
7
$\begingroup$

Two other approaches

With Indexed

expr1 = {-1, 1}*{Indexed[value, 1], Indexed[value, 2]}

expr1 /. {value -> {10, 20}}
(* {-10, 20} *)

With Inactivate and Activate

expr2 = Inactivate[{-1, 1}*value]

expr2 /. {value -> {10, 20}} // Activate
(* {-10, 20} *)

Hope this helps.

$\endgroup$
3
$\begingroup$

You could use a "custom multiplication" instead of times:

expr := CTimes[{-1, 1}, value]

Then you can replace the value and evaluate the expression after that by replacing CTimes by Times.

expr /. value -> {10, 20} /. CTimes -> Times
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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