จะคัดแยก / ดึงพลังบวกและลบออกมาเป็นรายการได้อย่างไร?
Aug 18 2020
ฉันมีนิพจน์ดังต่อไปนี้
expr = x1^d1 * x2^d2 * x3^d3;
โดยที่d1,d2,d3
สามารถมีค่า / นิพจน์เชิงลบกล่าวคือโดยทั่วไปสิ่งเหล่านี้d1,d2,d3
อาจไม่ใช่ค่าตัวเลข พูด
d1 = 4 d;
d2 = -5 e;
d3 = -6 d;
with assumption d, e are positive.
คำถาม: มีวิธีใดบ้างที่จะทำให้สองรายการหนึ่งมีเลขชี้กำลังเป็นบวกและอื่น ๆ ที่มีเลขชี้กำลังเป็นลบ
ความพยายามในปัจจุบันของฉันคือการใช้Exponent
สำหรับแต่ละคำศัพท์เหล่านี้และทดสอบทั้งด้านบวกและด้านลบ
คำตอบ
3 BobHanlon Aug 18 2020 at 01:42
$Version
(* "12.1.1 for Mac OS X x86 (64-bit) (June 19, 2020)" *)
Clear["Global`*"]
expr = x1^d1*x2^d2*x3^d3;
exponents =
Cases[expr, x_^p_. :> p, 1] /. {d1 -> 4 d, d2 -> -5 e, d3 -> -6 d}
(* {4 d, -5 e, -6 d} *)
pos = Assuming[{d > 0, e > 0}, Select[exponents, Simplify[# > 0] &]]
(* {4 d} *)
neg = Assuming[{d > 0, e > 0}, Select[exponents, Simplify[# < 0] &]]
(* {-5 e, -6 d} *)
1 kglr Sep 02 2020 at 05:45
นอกจากนี้คุณยังสามารถใช้Internal`SyntacticNegativeQ
กับGeneralUtilities`SelectDiscard
, GroupBy
, Cases
, DeleteCases
, Select
หรือPick
ดังนี้
expr = x1^d1*x2^d2*x3^d3
x1^(4 d) x2^(-5 e) x3^(-6 d)
exponents = Exponent[expr, {x1, x2, x3}]
{4 d, -5 e, -6 d}
{neg, pos} = GeneralUtilities`SelectDiscard[Internal`SyntacticNegativeQ] @ exponents
{neg, pos} = GroupBy[exponents, Internal`SyntacticNegativeQ] /@ {True, False}
{neg, pos} = Cases[#@_?Internal`SyntacticNegativeQ]@exponents & /@
{Identity, Except}
{neg, pos} = DeleteCases[#@_?Internal`SyntacticNegativeQ] @ exponents & /@
{Except, Identity}
{neg, pos} = Select[#@*Internal`SyntacticNegativeQ]@exponents & /@
{Identity, Not}
{neg, pos} = Pick[exponents,
Internal`SyntacticNegativeQ /@ exponents, #] & /@ {True, False}
ทั้งหมดให้
{{-5 e, -6 d}, {4 d}}