Solving an equation using a list of values for a parameter

The MWE below

Clear[p, x]
p = -Sqrt[2];
{x1, x2} = x /. Solve[p x^2 + (1 + p^2) x + p == 0, x]


solves the equation for the specific parameter $$p=-\sqrt2$$. How can I specify a list of $$p$$ values, e.g.

pvalues = {-Sqrt[2], Sqrt[2], -1/Sqrt[2], 1/Sqrt[2]}


and let Mathematica calculate the solutions for each $$p$$? TIA.

one of many ways

ClearAll[p, x];
pvalues = {-Sqrt[2], Sqrt[2], -1/Sqrt[2], 1/Sqrt[2]};
res = ( x /. Solve[# x^2 + (1 + #^2) x + # == 0, x]) & /@ pvalues


Update

if I want to use the output and calculate x1/x2 for each of the list pairs, how is this easily calculated?

There are many ways to do this. Here are some but I am sure more can be found. Assuming res is the above from the above, then any of these will work

(#[[1]]/#[[2]]) & /@ res
(First@#/Last@#) & /@ res
Map[#[[1]]/#[[2]] &, res]
Function[{x1, x2}, x1/x2] @@@ res
Apply[Function[{x1, x2}, x1/x2], res, {1}]


Each of the above commands gives

• Follow up; if I want to use the output and calculate x1/x2 for each of the list pairs, how is this easily calculated?
– mf67
Commented Sep 24, 2022 at 21:11
• @mf67 fyi, updated to answer your comment Commented Sep 24, 2022 at 23:43

Table is convenient.

Clear[sol];
pvalues = {-Sqrt[2], Sqrt[2], -1/Sqrt[2], 1/Sqrt[2]};
sol = Solve[p x^2 + (1 + p^2) x + p == 0, x]
x/.Table[sol, {p, pvalues}]
Table[(x /. sol[[1]])/(x /. sol[[2]]), {p, pvalues}]


Using SolveValues:

Map[Composition[SolveValues[#, x] &, # == 0 &, Function[{p}, p x^2 + (1 + p^2) x + p]], pvalues]


Or using SolveValues and Thread:

SolveValues[#, x] & /@ Thread[Function[{p}, p x^2 + (1 + p^2) x + p] /@ pvalues == 0]


Or more compact:

SolveValues[#, x] & /@ Function[{p}, p x^2 + (1 + p^2) x + p == 0] /@ pvalues


List /@ Thread[Rule[p, pvalues]]


generates the following rules:

$$\left\{\left\{p\to -\sqrt{2}\right\},\left\{p\to \sqrt{2}\right\},\left\{p\to -\frac{1}{\sqrt{2}}\right\},\left\{p\to \frac{1}{\sqrt{2}}\right\}\right\}$$

Each of these values for p can be applied to the equation:

Clear[x, p];
sol = x /. Solve[p x^2 + (1 + p^2) x + p == 0, x] /.

$$\left\{\frac{1}{\sqrt{2}},\sqrt{2}\right\},\left\{-\frac{1}{\sqrt{2}},-\sqrt{2}\right\},\left\{\sqrt{2},\frac{1}{\sqrt{2}}\right\},\left\{-\sqrt{2},-\frac{1}{\sqrt{2}}\right\}$$