A Tree? Multi-dimensional Array? hmm

Oh! First about the 0.2. That was a very stupid mistake in my code… You see that we compute T x to apply the transactions T to a vector x. To combine transactions T_1,\dots,T_n we need to compute T_n \cdots T_1 x, but wrongly I computed prod(transactions) which is T_1 \cdots T_n, i.e. in the wrong order.

The corrected code is

total_transaction = prod(reverse(transactions))

which then yields

julia> total_transaction[1:3, 1:3]
3×3 SparseMatrixCSC{Float64, Int64} with 7 stored entries:
  0.3   0.00205479   ⋅ 
 29.2   0.2          ⋅ 
  3.65  0.025       1.0

Sorry again for the mistake!


About the output, here is a quick code which gives your output. For sure, it could be done better but I guess it doesn’t matter too much:

using Printf

function print_transaction(T, rates, names = string.(1:size(T,1)))
    A = Matrix(T) 

    for col in 1:size(A, 2)
        targets = A[:, col]

        if any(targets .!= 0)
            printstyled("Transactions from [$(names[col])]:\n", color = :green)
            for row in 1:size(A,1)
                v = targets[row]
                p = targets[row] / rates[row, col] * 100
                if p != 0.0
                    @printf "  %.2f → %s       " p names[row] 
                    @printf "\t(1 %s → %.4f %s)\n" names[col] v names[row]
                end
            end
        end
    end
end

print_transaction(total_transaction, rates, ["USD", "JPY", "YUAN"])

gives

Transactions from [USD]:
  30.00 → USD           (1 USD → 0.3000 USD)
  20.00 → JPY           (1 USD → 29.2000 JPY)
  50.34 → YUAN          (1 USD → 3.6500 YUAN)
Transactions from [JPY]:
  30.00 → USD           (1 JPY → 0.0021 USD)
  20.00 → JPY           (1 JPY → 0.2000 JPY)
  50.00 → YUAN          (1 JPY → 0.0250 YUAN)
Transactions from [YUAN]:
  100.00 → YUAN         (1 YUAN → 1.0000 YUAN)

Note that the 50.34 percent are because the rates I got from google do not match exactly, i.e. you gain money when you convert a few times back and forth. With correct numbers that problem shouldn’t be there.

1 Like