1

The following code produces different results using Table and ParallelTable. K and KK are supposed to give the same result since rule is just also applied to K inside the table. However, the part using K with ParallelTable remains unReplaced for both z and s. This is like a minimal working example and I want to use With. What is wrong here? I'm using V13.1.

a = 0.2;
K = s Sqrt[(Sin[a]^2 - z^2)^2 - 4 (z^2 - Sin[a]^2)];
rule = z -> z0 + 4;
KK = K /. rule;

Table[With[{x = ({KK, K /. rule} /. s -> 1)}, x], {z0, 1, 3, 1}]

({{22.87325863762602,22.87325863762602},{33.90158745011456,33.\ 90158745011456},{46.917922210598825,46.917922210598825}})

ParallelTable[With[{x = ({KK, K /. rule} /. s -> 1)}, x], {z0, 1, 3, 1}]

({{22.87325863762602,s \ Sqrt[(0.039469502998557456 -z^2)^2-4
(-0.039469502998557456+z^2)]},{33.90158745011456,s
Sqrt[(0.039469502998557456-z^2)^2-4 \ (-0.039469502998557456+z^2)]},{46.917922210598825,s \ Sqrt[(0.039469502998557456 -z^2)^2-4
(-0.039469502998557456`+z^2)]}}
)

xiaohuamao
  • 4,728
  • 18
  • 36

1 Answers1

1

Use k instead of K, which is system reserved.

enter image description here

enter image description here

In general, (see here)

Avoid single-capital-letter names for your variables

Or in general, always avoid variables starting in capital-letters, as they are likely to collide with system functions or variables.

This modified code works:

a = 0.2;
k = s Sqrt[(Sin[a]^2 - z^2)^2 - 4 (z^2 - Sin[a]^2)];
rule = z -> z0 + 4;
kk = k /. rule;

ParallelTable[ With[ { x = ({kk, k /. rule} /. s -> 1) } , x ] , {z0, 1, 3, 1} ]

(* {{22.8733, 22.8733}, {33.9016, 33.9016}, {46.9179, 46.9179}} *)

Also, please consider writing tidier code, using scoping and fewer variable assignments.

rhermans
  • 36,518
  • 4
  • 57
  • 149