Unexpected result when indexing a multidimensional List in Dart - dart

I'm getting a strange result when indexing a multi-dimensional list. Take the following for example:
void main() {
var multiList = new List.filled(4, new List.filled(4, "x"));
print(multiList);
// [[x, x, x, x], [x, x, x, x], [x, x, x, x], [x, x, x, x]]
multiList[2][1] = "A";
print(multiList);
// [[x, A, x, x], [x, A, x, x], [x, A, x, x], [x, A, x, x]]
// Expected result:
// [[x, x, x, x], [x, x, x, x], [x, A, x, x], [x, x, x, x]]
}
When I try to replace index [2][1] with a value, the value is placed in all the sub-Lists.
Do you know why this is happening? How can I get the expected result instead?
Thank for your help!

I think you need to use generate instead of filled
var multiList = new List.generate(4, (i) => new List.filled(4, "x"));
otherwise only one new List.filled(4, "x") is created and inserted 4 times. If you add a non-primitive type in the inner list then use generate there too.

Related

Maxima: eliminate variables from equations

Given N equations in K variables,
can Maxima produce N-J equations in K-J variables?
SOLVE and ELIMINATE seem unable, about to reach for pen and paper.
(%i1) elim ([a = x + y, b = y + z, c = z + x, a = b * c], [a, b, c]);
(%o1) elim([a = y + x, b = z + y, c = z + x, a = b c], [a, b, c])
(%i2) load (to_poly);
(%o2) ~/maxima-5.44.0/share/to_poly_solve/to_poly.lisp
(%i3) elim ([a = x + y, b = y + z, c = z + x, a = b * c], [a, b, c]);
2
(%o3) [[z + (y + x) z + (x - 1) y - x],
[b z - y + b x - x, z + x - c, y + x - a]]
(%i4) solve (first (%o3), x);
2
z + y z - y
(%o4) [x = - ------------]
z + y - 1

Cartesian product in Dart Language

How can I create Cartesian Product of dynamic number of lists in Dart Language ?
For example I have two lists:
X: [A, B, C]; Y: [W, X, Y, Z]
I want to create lists like this [AW, AX, AY, AZ, BW, BX, BY, BZ, CW, CX, CY, CZ]
Although Python, Java have pre implemented libraries, there is none for Dart language I think.
Tested with Dart 2.5.0:
class PermutationAlgorithmStrings {
final List<List<String>> elements;
PermutationAlgorithmStrings(this.elements);
List<List<String>> permutations() {
List<List<String>> perms = [];
generatePermutations(elements, perms, 0, []);
return perms;
}
void generatePermutations(List<List<String>> lists, List<List<String>> result, int depth, List<String> current) {
if (depth == lists.length) {
result.add(current);
return;
}
for (int i = 0; i < lists[depth].length; i++) {
generatePermutations(lists, result, depth + 1, [...current, lists[depth][i]]);
}
}
}
You can input any length of string arrays as you like.
Use like this:
PermutationAlgorithmStrings algo = PermutationAlgorithmStrings([
["A", "B", "C"],
["W", "X", "Y", "Z"],
["M", "N"]
]);
Output:
output: [[A, W, M], [A, W, N], [A, X, M], [A, X, N], [A, Y, M], [A, Y, N], [A, Z, M], [A, Z, N], [B, W, M], [B, W, N], [B, X, M], [B, X, N], [B, Y, M], [B, Y, N], [B, Z, M], [B, Z, N], [C, W, M], [C, W, N], [C, X, M], [C, X, N], [C, Y, M], [C, Y, N], [C, Z, M], [C, Z, N]]
You can write this as a simple list:
var product = [for (var x in X) for (var y in Y) "$x$y"];
(assuming X and Y contain strings and the combination you want is concatenation, otherwise write something else than "$x$y" to combine the x and y values).
For an arbitrary number of lists, it gets more complicated. I'd probably prefer to generate the combinations lazily, instead of having to keep all the lists in memory at the same time if it isn't necessary. You can always create them eagerly if needed.
Maybe try something like:
Iterable<List<T>> cartesian<T>(List<List<T>> inputs) sync* {
if (inputs.isEmpty) {
yield List<T>(0);
return;
}
var indices = List<int>.filled(inputs.length, 0);
int cursor = inputs.length - 1;
outer: do {
yield [for (int i = 0; i < indices.length; i++) inputs[i][indices[i]]];
do {
int next = indices[cursor] += 1;
if (next < inputs[cursor].length) {
cursor = inputs.length - 1;
break;
}
indices[cursor] = 0;
cursor--;
if (cursor < 0) break outer;
} while (true);
} while (true);
}
Functional solve.
//declare type matters!
List<List<dynamic>> cards = [
[1, 2, 3],
[4, 5],
['x','y']
];
cartesian product
//or List flatten(List iterable) => iterable.expand((e) => e is List ? flatten(e) : [e]).toList(); // toList() cannot omit
Iterable flatten(Iterable iterable) => iterable.expand((e) => e is Iterable ? flatten(e) : [e]);
//cannot omit paramenter type
List<List<dynamic>> cartesian(List<List<dynamic>> xs) =>
xs.reduce((List<dynamic> acc_x, List<dynamic> x) => // type cannot be omit
acc_x.expand((i) => x.map((j) => flatten([i, j]).toList())).toList());
Maybe use Dart dynamic type is silly, you can use type friendly version
I quit using reduce function because of its strict dimension limiting on parameters as well as returning values
Type friendly
List<List<T>> cartesian<T>(List<List<T>> list) {
var head = list[0];
var tail = list.skip(1).toList();
List<List<T>> remainder = tail.length > 0 ? cartesian([...tail]) : [[]];
List<List<T>> rt = [];
for (var h in head) {
for (var r in remainder)
rt.add([h, ...r]);
}
return rt;
}
Try with this solution:
void main() {
List<String> a = ['A','B','C'];
List<String> b = ['X','Y','Z'];
List<String> c = a.map((ai) => b.map((bi) => ai+bi).toList()).expand((i) => i).toList();
c.forEach((ci) => print(ci));
}

Find the shortest path in DLV

I am trying to find all the paths in a graph with minimum distance using DLV. Say I have the following graph:
I am expecting to obtain the predicates (I hope I don't skip any):
path(a, b, 1), path(a, d, 1), path(a, e, 1), path(a, c, 2)
path(b, a, 1), path(b, c, 1), path(d, d, 2), path(b, e, 2)
path(c, b, 1), path(c, e, 1), path(c, a, 2), path(c, d, 3)
path(d, a, 1), path(d, b, 2), path(d, e, 2), path(d, c, 3)
path(e, a, 1), path(e, c, 1), path(e, d, 2), path(e, b, 2)
I assume that you can travel an arch both left or right. So, I tried the following:
path(X, Y, 1) :- arc(X, Y).
path(Y, X, 1) :- arc(X, Y).
path(X, Z, L) :- path(X, Y, M), path(Y, Z, N),
X!=Z,
L = M + N,
not path(X, Z, V), V < L, #int(V)
The idea of the third rule was to add 2 existing paths if they are not going back (X!=Z) and there is not already a path connecting the same edges with a shorter distance (not path(X, Z, V), V < L, #int(V)). I had to add #int(V) because otherwise the rule was not safe. I don't know if there is a better way of resolving this safety issue with an integer value.
When I run this code (with the flag -N=5 to set #maxint=5) I get paths that should not be there, for example, path(d,a,5). I don't know if the problem is with the #int(V) or something else but I wouldn't expect these paths to appear since I already have a path(d,a,1). Probably it is because of #int(V) but I can't figure out how to do this right.
Can anyone help me solve this? Thanks in advance.
Solution to the problem using lists to keep track of the path:
path(X, Y, [X, Y], 1) :- arc(X, Y).
path(Y, X, [Y, X], 1) :- arc(X, Y).
path(X, Z, P, D) :- path(X, Y, P1, D1),
path(Y, Z, P2, 1),
#insLast(P1, Z, P),
D = D1 + 1,
not #member(Z, P1).
shortest_path(X, Y, D) :- node(X), node(Y),
#min{L: path(X, Y, P, L)} = D.
Solution without the need of lists (with the help of CapelliC)
path(X, Y, 1) :- arc(X,Y).
path(Y, X, 1) :- arc(X,Y).
path(X, Y, D) :- path(X,Z,D0), arc(Z,Y),
#count{A: node(A)} = Max,
D0<Max, X != Y,
D = D0+1.
shorter_paths(X, Y, D) :- node(X), node(Y),
#min{L: path(X, Y, L)} = D.
Note that we need to define all nodes with a predicate node() and that the predicate arc() assumes that the edge of the graph is bidirectional.
examples/spaths.dl from DES distribution. See the commented code below... -
%
% Shortest Paths in a Graph
%
% Datalog Formulation
%
% Program: Shortest paths in a graph
% Author : Fernando Sáenz-Pérez
% Date : September, 2009
edge(a,b).
edge(a,c).
edge(b,a).
edge(b,d).
path(X,Y,1) :-
edge(X,Y).
path(X,Y,L) :-
path(X,Z,L0),
edge(Z,Y),
count(edge(A,B),Max),
L0<Max,
L is L0+1.
spaths(X,Y,L) :-
min(path(X,Y,Z),Z,L).
% Note that the following is not stratifiable in DES
%sp(X,Y,1) :-
% edge(X,Y).
%sp(X,Y,L) :-
% sp(X,Z,L0),
% not(shorter(X,Z,L0)),
% edge(Z,Y),
% L is L0+1.
%shorter(X,Y,L) :-
% sp(X,Y,L0),
% L0<L.

How to reduce memory useage when solving system of 9 coupled PDE

I have a system of coupled partial differential equations as shown in 1.
system of equations
If one writes the equations componentwise, one gets nine equations.
For simplicity, one can set $\rho$ and $h$ constant.
I put the equations into a mathematica notebook and used 'NDSolve'.
I counted a number of 268 derivatives in the system. However, I didnt find more than 87 conditions that made sence.
When I run the notebook, I get the message
NDSolve::pdord: Some of the functions have zero differential order, so the equations will be solved as a system of differential-algebraic equations. >>
LinearSolve::sing: Matrix SparseArray[Automatic,<<3>>] is singular. >>
NDSolve::mconly: For the method IDA, only machine real code is available. Unable to continue with complex values or beyond floating-point exceptions. >>
I understand the first error: There is no time derivative in $\phi$, so the system is strictly speaking a differential-algebraic-system.
After about 40 minutes, mathematica says:
No more memory available.
Mathematica kernel has shut down.
Try quitting other applications and then retry.
What can I change to reduce the memory cost, or -- better -- what can I change to solve the system more elegant?
Here is the mathematica code:
NDSolve[{
eqn1 == 0,
eqn2 == 0,
eqn3 == 0,
eqn4 == 0,
eqn5 == 0,
eqn6 == 0,
eqn7 == 0,
eqn8 == 0,
eqn9 == 0,
(*Initial Conditions*)
T[0, x, y, z] == 300,
p[0, x, y, z] == 101300,
phi[t, x, y, z] == 75 x /. t -> 0,
u1[0, x, y, z] == 0,
(* ect... *)
(*BOUNDARY CONDITIONS*)
phi[t, x, y, z] == 0 /. x -> 0,
phi[t, x, y, z] == 750 /. x -> 10,
phi[t, x, y, z] == 75 x /. y -> 0,
phi[t, x, y, z] == 75 x /. y -> 10,
phi[t, x, y, z] == 75 x /. z -> 0,
phi[t, x, y, z] == 75 x /. z -> 10,
\!\(
\*SubscriptBox[\(\[PartialD]\), \(y\)]\(phi[t, x, y, z]\)\) == 0 /.
y -> 0,
\!\(
\*SubscriptBox[\(\[PartialD]\), \(y\)]\(phi[t, x, y, z]\)\) == 0 /.
y -> 10,
(* ect..... *)
},
{p, u1, u2, u3, T, phi, a1, a2, a3},
{t, 0, 10}, {x, 0, 10}, {y, 0, 10}, {z, 0, 10}
]
And here is the Latex-code for the equations:
\begin{align}
%%% first
\frac{\partial \rho}{\partial t} + \vec{u} \cdot \nabla \rho + \rho \nabla \cdot \vec{u} &=0 \\
%%% second
\rho \frac{\partial \vec{u}}{\partial t}
+ \rho \vec{u} \cdot \nabla \vec{u} \pm \nabla p
+ \nabla \cdot \left[- \mu \left( \nabla \vec{u} + \nabla \vec{u}^t \right) \right] \nonumber
\\ -
\left[ \sigma
\left( - \nabla \phi - \frac{\partial \vec{A}}{\partial t} \right)
+ \vec{u} \times (\nabla \times \vec{A}) \right]
%
\times \left[ \nabla \times \vec{A} \right]
&= \vec{0} \\
%%% third
\rho \frac{\partial h}{\partial t}
+ \rho \vec{u} \cdot \nabla h
+ \nabla \cdot \nabla T
- \frac{\partial p}{\partial t}
- \overset{.}{Q_j} + \overset{.}{Q_r}
&= 0 \\
%%% fourth
\nabla \cdot \left[ \sigma
\left( - \nabla \phi - \frac{\partial \vec{A}}{\partial t} \right)
+ \vec{u} \times (\nabla \times \vec{A}) \right] &= 0 \\
%%% fifth
\frac{\partial \vec{A}}{\partial t} + \nabla \phi - \vec{u} \times \nabla \times \vec{A} - \eta \nabla ^2 \vec{A} &= \vec{0}
\end{align}
I add the plain mathematica code here:
Needs["NDSolve`FEM`"]
kB = N[ 8.6*10^-5 ];
AG = N[1.2*10^6 ];
mu0 = 1.257*10^(-6);
sigma = N[20.];
eta = 1/(mu0 * sigma);
rho = N[1.];
h = N[1672000.];
r = 1/ 287.058 ;
mu = N[1.];
kappah = N[5.];
cp = N[2.4];
lbb = NDSolve[{
0 == D[rho, t] + u1[t, x, y, z]*D[rho, x] +
u2[t, x, y, z]*D[rho, y] + u3[t, x, y, z]*D[rho, z] +
rho*(D[u1[t, x, y, z], x] + D[u2[t, x, y, z], y] +
D[u3[t, x, y, z], z]),
0 ==
rho*D[u1[t, x, y, z], t] +
rho*u1[t, x, y, z]*D[u1[t, x, y, z], x] - D[p[t, x, y, z], x] -
mu*D[D[u1[t, x, y, z], x], x] -
mu*D[D[u1[t, x, y, z], y], y] -
mu*D[D[u1[t, x, y, z], z], z] -
(((-sigma)*D[phi[t, x, y, z], y] -
sigma*D[a2[t, x, y, z], t] +
u3[t, x, y,
z]*(D[a3[t, x, y, z], y] - D[a2[t, x, y, z], z]) -
u1[t, x, y,
z]*(D[a2[t, x, y, z], x] - D[a1[t, x, y, z], y]))*(D[
a2[t, x, y, z], x] - D[a1[t, x, y, z], y]) -
((-sigma)*D[phi[t, x, y, z], z] -
sigma*D[a3[t, x, y, z], t] +
u1[t, x, y,
z]*(D[a1[t, x, y, z], z] - D[a3[t, x, y, z], x]) -
u2[t, x, y,
z]*(D[a3[t, x, y, z], y] - D[a2[t, x, y, z], z]))*(D[
a1[t, x, y, z], z] - D[a3[t, x, y, z], x])),
0 ==
rho*D[u2[t, x, y, z], t] +
rho*u2[t, x, y, z]*D[u2[t, x, y, z], y] - D[p[t, x, y, z], y] -
mu*D[D[u2[t, x, y, z], x], x] -
mu*D[D[u2[t, x, y, z], y], y] -
mu*D[D[u2[t, x, y, z], z], z] -
(((-sigma)*D[phi[t, x, y, z], z] -
sigma*D[a3[t, x, y, z], t] +
u1[t, x, y,
z]*(D[a1[t, x, y, z], z] - D[a3[t, x, y, z], x]) -
u2[t, x, y,
z]*(D[a3[t, x, y, z], y] - D[a2[t, x, y, z], z]))*(D[
a3[t, x, y, z], y] - D[a2[t, x, y, z], z]) -
(D[a2[t, x, y, z], x] -
D[a1[t, x, y, z], y])*((-sigma)*D[phi[t, x, y, z], x] -
sigma*D[a1[t, x, y, z], t] +
u2[t, x, y,
z]*(D[a2[t, x, y, z], x] - D[a1[t, x, y, z], y]) -
u3[t, x, y,
z]*(D[a1[t, x, y, z], z] - D[a3[t, x, y, z], x]))),
0 ==
rho*D[u3[t, x, y, z], t] +
rho*u3[t, x, y, z]*D[u3[t, x, y, z], z] - D[p[t, x, y, z], z] -
mu*D[D[u3[t, x, y, z], x], x] -
mu*D[D[u3[t, x, y, z], y], y] -
mu*D[D[u3[t, x, y, z], z], z] -
(((-sigma)*D[phi[t, x, y, z], x] -
sigma*D[a1[t, x, y, z], t] +
u2[t, x, y,
z]*(D[a2[t, x, y, z], x] - D[a1[t, x, y, z], y]) -
u3[t, x, y,
z]*(D[a1[t, x, y, z], z] - D[a3[t, x, y, z], x]))*(D[
a1[t, x, y, z], z] - D[a3[t, x, y, z], x]) -
((-sigma)*D[phi[t, x, y, z], y] -
sigma*D[a2[t, x, y, z], t] +
u3[t, x, y,
z]*(D[a3[t, x, y, z], y] - D[a2[t, x, y, z], z]) -
u1[t, x, y,
z]*(D[a2[t, x, y, z], x] - D[a1[t, x, y, z], y]))*(D[
a3[t, x, y, z], y] - D[a2[t, x, y, z], z])),
0 == rho*D[h, t] +
rho*(u1[t, x, y, z]*D[h, x] + u2[t, x, y, z]*D[h, y] +
u3[t, x, y, z]*D[h, z]) +
kappah*(D[T[t, x, y, z], x, x] + D[D[T[t, x, y, z], y], y] +
D[D[T[t, x, y, z], z], z]) - D[p[t, x, y, z], t],
0 == D[
sigma*(-D[phi[t, x, y, z], x] - D[a1[t, x, y, z], t]) +
u2[t, x, y, z]*(D[a2[t, x, y, z], x] - D[a1[t, x, y, z], y]) -
u3[t, x, y, z]*(D[a1[t, x, y, z], z] - D[a3[t, x, y, z], x]),
x] + D[sigma*(-D[phi[t, x, y, z], y] - D[a2[t, x, y, z], t]) +
u3[t, x, y, z]*(D[a3[t, x, y, z], y] - D[a2[t, x, y, z], z]) -
u1[t, x, y, z]*(D[a2[t, x, y, z], x] - D[a1[t, x, y, z], y]),
y] +
D[sigma*(-D[phi[t, x, y, z], z] - D[a3[t, x, y, z], t]) +
u1[t, x, y, z]*(D[a1[t, x, y, z], z] - D[a3[t, x, y, z], x]) -
u2[t, x, y, z]*(D[a3[t, x, y, z], y] - D[a2[t, x, y, z], z]),
z],
0 ==
D[a1[t, x, y, z], t] + D[phi[t, x, y, z], x] -
u2[t, x, y, z]*(D[a2[t, x, y, z], x] - D[a1[t, x, y, z], y]) -
u3[t, x, y, z]*(D[a1[t, x, y, z], z] - D[a3[t, x, y, z], x]) -
eta*D[D[a1[t, x, y, z], x], x],
0 ==
D[a2[t, x, y, z], t] + D[phi[t, x, y, z], y] -
u3[t, x, y, z]*(D[a3[t, x, y, z], y] - D[a2[t, x, y, z], z]) -
u1[t, x, y, z]*(D[a2[t, x, y, z], x] - D[a1[t, x, y, z], y]) -
eta*D[D[a2[t, x, y, z], y], y],
0 ==
D[a3[t, x, y, z], t] + D[phi[t, x, y, z], z] -
u1[t, x, y, z]*(D[a1[t, x, y, z], z] - D[a3[t, x, y, z], x]) -
u2[t, x, y, z]*(D[a3[t, x, y, z], y] - D[a2[t, x, y, z], z]) -
eta*D[D[a3[t, x, y, z], z], z], T[0, x, y, z] == 300,
p[0, x, y, z] == 101300,
phi[t, x, y, z] == 75*x /. t -> 0, u1[0, x, y, z] == 0,
u2[0, x, y, z] == 0, u3[0, x, y, z] == 0,
a1[0, x, y, z] == 0, a2[0, x, y, z] == 0,
a3[0, x, y, z] == 0, D[T[t, x, y, z], x] == 0 /. x -> 0,
D[T[t, x, y, z], x] == 0 /. x -> 10,
D[T[t, x, y, z], y] == 0 /. y -> 0,
D[T[t, x, y, z], y] == 0 /. y -> 10,
D[T[t, x, y, z], z] == 0 /. z -> 0,
D[T[t, x, y, z], z] == 0 /. z -> 10,
phi[t, x, y, z] == 0 /. x -> 0,
phi[t, x, y, z] == 750 /. x -> 10,
phi[t, x, y, z] == 75*x /. y -> 0,
phi[t, x, y, z] == 75*x /. y -> 10,
phi[t, x, y, z] == 75*x /. z -> 0,
phi[t, x, y, z] == 75*x /. z -> 10,
D[phi[t, 0, y, z], x] == (-sigma^(-1))*0.5*AG*T[t, 0, y, z]^2*
Exp[-4/(kB*T[t, 0, y, z])],
D[phi[t, 10, y, z], x] == (-sigma^(-1))*0.5*AG*T[t, 10, y, z]^2*
Exp[-4/(kB*T[t, 10, y, z])],
D[phi[t, x, y, z], y] == 0 /. y -> 0,
D[phi[t, x, y, z], y] == 0 /. y -> 10,
D[phi[t, x, y, z], z] == 0 /. z -> 0,
D[phi[t, x, y, z], z] == 0 /. z -> 10,
p[t, x, y, z] == 101300 /. x -> 0,
p[t, x, y, z] == 101300 /. x -> 10,
p[t, x, y, z] == 101300 /. y -> 0,
p[t, x, y, z] == 101300 /. y -> 10,
p[t, x, y, z] == 101300 /. z -> 0,
p[t, x, y, z] == 101300 /. z -> 10, u1[t, x, y, z] == 0 /. x -> 0,
u1[t, x, y, z] == 0 /. x -> 10,
D[u1[t, x, y, z], y] == 0 /. y -> 0,
D[u1[t, x, y, z], y] == 0 /. y -> 10,
D[u1[t, x, y, z], z] == 0 /. z -> 0,
D[u1[t, x, y, z], z] == 0 /. z -> 10,
u2[t, x, y, z] == 0 /. x -> 0,
u2[t, x, y, z] == 0 /. x -> 10,
D[u2[t, x, y, z], y] == 0 /. y -> 0,
D[u2[t, x, y, z], y] == 0 /. y -> 10,
D[u2[t, x, y, z], z] == 0 /. z -> 0,
D[u2[t, x, y, z], z] == 0 /. z -> 10,
u3[t, x, y, z] == 0 /. x -> 0, u3[t, x, y, z] == 0 /. x -> 10,
D[u3[t, x, y, z], y] == 0 /. y -> 0,
D[u3[t, x, y, z], y] == 0 /. y -> 10,
D[u3[t, x, y, z], z] == 0 /. z -> 0,
D[u3[t, x, y, z], z] == 0 /. z -> 10,
a1[t, x, y, z] == 0 /. x -> 0,
a1[t, x, y, z] == 0 /. x -> 10,
a1[t, x, y, z] == 0 /. y -> 0,
a1[t, x, y, z] == 0 /. y -> 10,
a1[t, x, y, z] == 0 /. z -> 0,
a1[t, x, y, z] == 0 /. z -> 10,
D[a1[t, x, y, z], x] == 0 /. x -> 0,
D[a1[t, x, y, z], x] == 0 /. x -> 10,
D[a1[t, x, y, z], y] == 0 /. y -> 0,
D[a1[t, x, y, z], y] == 0 /. y -> 10,
D[a1[t, x, y, z], z] == 0 /. z -> 0,
D[a1[t, x, y, z], z] == 0 /. z -> 10,
a2[t, x, y, z] == 0 /. x -> 0,
a2[t, x, y, z] == 0 /. x -> 10,
a2[t, x, y, z] == 0 /. y -> 0,
a2[t, x, y, z] == 0 /. y -> 10,
a2[t, x, y, z] == 0 /. z -> 0,
a2[t, x, y, z] == 0 /. z -> 10,
D[a2[t, x, y, z], x] == 0 /. x -> 0,
D[a2[t, x, y, z], x] == 0 /. x -> 10,
D[a2[t, x, y, z], y] == 0 /. y -> 0,
D[a2[t, x, y, z], y] == 0 /. y -> 10,
D[a2[t, x, y, z], z] == 0 /. z -> 0,
D[a2[t, x, y, z], z] == 0 /. z -> 10,
a3[t, x, y, z] == 0 /. x -> 0,
a3[t, x, y, z] == 0 /. x -> 10,
a3[t, x, y, z] == 0 /. y -> 0,
a3[t, x, y, z] == 0 /. y -> 10,
a3[t, x, y, z] == 0 /. z -> 0,
a3[t, x, y, z] == 0 /. z -> 10,
D[a3[t, x, y, z], x] == 0 /. x -> 0,
D[a3[t, x, y, z], x] == 0 /. x -> 10,
D[a3[t, x, y, z], y] == 0 /. y -> 0,
D[a3[t, x, y, z], y] == 0 /. y -> 10,
D[a3[t, x, y, z], z] == 0 /. z -> 0,
D[a3[t, x, y, z], z] == 0 /. z -> 10
},
{p, u1, u2, u3, T, phi, a1, a2, a3},
{t, 0, 10}, {x, 0, 10}, {y, 0, 10}, {z, 0, 10}]
Thanks a lot!

How do you return multiple values and assign them to mutable variables?

This is what I have so far.
let Swap (left : int , right : int ) = (right, left)
let mutable x = 5
let mutable y = 10
let (newX, newY) = Swap(x, y) //<--this works
//none of these seem to work
//x, y <- Swap(x, y)
//(x, y) <- Swap(x, y)
//(x, y) <- Swap(x, y)
//do (x, y) = Swap(x, y)
//let (x, y) = Swap(x, y)
//do (x, y) <- Swap(x, y)
//let (x, y) <- Swap(x, y)
You can't; there's no syntax to update 'more than one mutable variable' with a single assignment. Of course you can do
let newX, newY = Swap(x,y)
x <- newX
y <- newY
F# has "by reference" parameters just like C#, so you can write a classic swap function similarly:
let swap (x: byref<'a>) (y: byref<'a>) =
let temp = x
x <- y
y <- temp
let mutable x,y = 1,2
swap &x &y
The code you have commented doesn't work because when you write "x, y" you create a new tuple that is an immutable value, so can't be updated. You could create a mutable tuple and then overwrite it with the result of the swap function if you want:
let mutable toto = 5, 10
let swap (x, y) = y, x
toto <- swap toto
My advice would be to investigate the immutable side of F#, look at the ways you can use immutable structures to achieve what you previously would have done using mutable values.
Rob

Resources