One puzzle, two solutions

By: Blog by Bogumił Kamiński

Re-posted from: https://bkamins.github.io/julialang/2024/02/09/pe116.html

Introduction

Today, I wanted to switch back to a lighter subject.
Therefore I decided to have a look at my favorite Project Euler website.

I picked the problem 116 as I have not tried to solve it yet.
Interestingly, it turned out that there are two ways to approach this puzzle,
so I thought to share them here.

The post was written under Julia 1.10.0.

The puzzle

The Project Euler puzzle 116 can be briefly stated as follows:

Given a row of 50 grey squares is to have a number of its tiles replaced with
coloured oblong tiles chosen from red (length two),
green (length three), or blue (length four).
How many different ways can the grey tiles be replaced if colours
cannot be mixed and at least one coloured tile must be used?

(If you want to see some visual examples of valid tilings, I encourage you to
visit the puzzle 116 page.)

The first approach

When we think of this problem, it is natural to generalize it. By C(n, d) we can
define the number of ways that n gray squares can be replaced with tiles of length d.
Then the solution to our problem is C(n, 2) + C(n, 3) + C(n, 4).
So let us focus on computing C(n, d) (assuming d is positive).

The first approach is to ask how many tiles of length d can be put. There must be at least
1, and we cannot put more than n ÷ d (here I use the ÷ notation taken from Julia that
denotes integer division; in other words the integer part of n / d).

So now assume that we want to put i blocks of length d (assuming i is valid). In
how many ways can we do it. Well, we put i long blocks and we are left with n - d*i gray blocks.
In total we have i + (n - d*i) blocks. You can then think of it as you have that many slots
from which you need pick i slots to put the long blocks. The number of ways you can do it is
given by the value of binomial coefficient. In Julia notation it is:
binomial(BigInt(i + (n - d*i)), BigInt(i)).

Now you might ask why I put the BigInt wrapper around the passed numbers? The reason is
that binomial coefficient gets large pretty quickly, so I want to make sure I will not
have issues with integer overflow.

Given these considerations the first function that produces C(n, d) can be defined as:

function C1(n::Integer, d::Integer)
    @assert d > 0 && n >= 0
    return sum(i -> binomial(BigInt(i + (n - d*i)), BigInt(i)), 1:n ÷ d; init=big"0")
end

Note that I use the init=big"0" initialization statement in the sum to ensure the
correct handling of the cases when n < d when we are given an empty collection to sum over.

The second approach

However, there is a different way how we can think of computing C(n, d).

Assume we know the values of C(n, d) for values of n smaller than the requested one.

We look at the last tile in our row.

If it is empty, then we are down to n-1 tiles to be filled.
This can be done in C(n-1, d) ways (remember that this value takes care
of the fact that at least one block of length d has to be used).

But what if the last tile in our row is filled with a block of length d?
Then we have two options. Either all other blocks are left gray (which gives us 1 combination)
or we are left with n-d tiles that are filled with at least one block of length d. The
second value is exactly C(n-d, d).

In summary we get that C(n, d) = C(n-1, d) + C(n-d, d) + 1.

This formula assumes n is at least d. But clearly for n < d
we have 0 ways to arrange the blocks.

Let us write down the code that performs the required computation:

function C2(n::Integer, d::Integer)
    @assert d > 0 && n >= 0
    npos = Dict{Int,BigInt}(i => 0 for i in 0:d-1)
    for j in d:n
        npos[j] = npos[j-1] + npos[j-d] + 1
    end
    return npos[n]
end

Note in the code that I used the npos dictionary to flexibly allow
for any potential integer values of n. The dictionary has
Dict{Int,BigInt} type, again, to ensure that the results of the computations
are stored correctly even if they are large.

Testing

Now we have two functions C1 and C2 that look completely differently.
Do they produce the same results. Let us check:

julia> using Test

julia> @testset "test C1 and C2 equality" begin
           for n in 0:200, d in 1:20
               @test C1(n, d) == C2(n, d)
           end
       end;
Test Summary:           | Pass  Total  Time
test C1 and C2 equality | 4020   4020  0.9s

Indeed we see that both C1 and C2 functions produce the same results.

To convince ourselves that using arbitrary precision integers was indeed needed
let us check some example values of the functions:

julia> C1(200, 2)
453973694165307953197296969697410619233825

julia> C2(200, 2)
453973694165307953197296969697410619233825

julia> typemax(Int)
9223372036854775807

Indeed, if we were not careful, we would have an integer overflow issue.

Conclusions

As usual I will not show the value of the solution to the problem to encourage you
to run the code yourself. You can get it by executing either
sum(d -> C1(50, d), 2:4) or sum(d -> C2(50, d), 2:4).
(We have just checked that the value produced in both cases is the same).

JuliaSim Battery Simulation 2024 | JuliaHub

By: JuliaHub

Re-posted from: https://info.juliahub.com/blog/newsletter-february-2024-next-gen-battery-simulation-with-juliasim

Next Gen Battery Simulation with JuliaSim: Dr. Marc Berliner (JuliaSim Batteries Lead Developer) and Dr. Chris Rackauckas (JuliaHub VP Modeling and Simulation) are leading a free Webinar Thursday February 22 on Next Gen Battery Simulation: Solving 1,000 Cell Electrochemical Battery Packs with JuliaSim. This Webinar is free but registration is required.

Functional Mockup Unit and Genie Builder Webinars: Other free February Webinars sponsored by JuliaHub include Ingesting and Deploying Functional Mockup Units in JuliaSim with Dr. Ranjan Anantharaman (JuliaHub Sales Engineer) and Build Data-Centric Web Applications in Julia with Genie Builder with Dr. Pere Giménez (Genie Data Scientist and Developer Advocate) and Adrian Salceanu (Genie Co-Founder and Genie Framework Creator). These Webinars are free but registration is required.

Webinar

Presenter

Date and Time

Register

Ingesting and Deploying Functional Mockup Units in JuliaSim

Dr. Ranjan Anantharaman, JuliaHub Sales Engineer

Thursday February 15

1-2 pm Eastern (US)

Link

Next Gen Battery Simulation: Solving 1,000 Cell Electrochemical Battery Packs with JuliaSim

Dr. Marc Berliner, JuliaSim Batteries Lead Developer and Dr. Chris Rackauckas, JuliaHub VP Modeling and Simulation

Thursday February 22 1-2 pm Eastern (US)

Link

Build Data-Centric Web Applications in Julia with Genie Builder

Dr. Pere Giménez, Genie Data Scientist and Developer Advocate and Adrian Salceanu, Genie Co-Founder and Genie Framework Creator

Thursday February 29 10-11 am Eastern (US)

Link

Comparative Analysis of Cell Chemistries with JuliaSim Batteries

Dr. Marc Berliner, JuliaSim Batteries Lead Developer

Tuesday March 5  1-2 pm Eastern (US)

 

Link

Preparing for Manufacturing Defects with JuliaSim Batteries

Dr. Marc Berliner, JuliaSim Batteries Lead Developer and Dr. Ranjan Anantharaman, JuliaHub Sales Engineer

Tuesday March 19  1-2 pm Eastern (US)

Link

Julia Growth Statistics: Julia use and awareness continue to grow rapidly.

 

Total Through Jan 2016

Total Through Jan 2024

Growth

Cumulative GitHub Stars – Julia + Julia Packages

18,882

440,525

23x

Number of Registered Julia Packages

690

10,356

15x

Published Citations of Julia: A Fast Dynamic Language for Technical Computing (2012), Julia: A Fresh Approach to Numerical Computing (2017) and Julia: Dynamism and Performance Reconciled by Design (2018)

143

6,650

47x

Number of News Articles Mentioning Julia, JuliaHub or Julia Computing

14

1,375

98x

Discourse Views

329,918 (Jan 2017)

104,733,784

317x

Julia Language YouTube Channel Minutes Watched

1,331,090

39,421,884

30x

 

JuliaCon 2024: JuliaCon 2024 takes place from July 9-13 in Eindhoven, Netherlands. Workshops will take place Tuesday July 9, presentations Wednesday July 10 through Friday July 12 and the hackathon will be Saturday July 13.

Julia 1.10 Released: Julia 1.10 contains a number of new and improved features including:

  • New parser written in Julia
  • Package load time improvements
  • Improvements in stacktrace rendering
  • Parallel garbage collection
  • Tracy and Intel VTune ITTAPI profiling integration
  • Upgrade to LLVM 15
  • Linux AArch64 stability improvements
  • Parallel native code generation for system images and package images
  • Avoiding races during parallel precompilation
  • Parallel precompile on using

More information is available in Julia 1.10 Highlights.

Why Julia? Do you have a colleague who wants to know what makes Julia different, better and more exciting? Why Julia? is a new white paper from JuliaHub that can help answer these questions. Click here to read it.

Build a Data-Rich Dashboard App on JuliaHub: JuliaHub’s Michael Bologna published a new blog post describing how to Build a Data-Rich Dashboard App on JuliaHub. Click here to learn more.

JuliaHub CEO and Co-Founder Dr. Viral Shah with Forbes India: Forbes India’s Harichandan Arikali interviewed JuliaHub CEO and co-founder Dr. Viral Shah for the Startup Fridays podcast. Click to watch Viral Shah on the Journey of Julia Programming Language to Tackle the ‘Two Language Problem’ or listen to Viral Shah on the Mission at JuliaHub to Help Scientists and Engineers Innovate Faster.

Julia Monthly Newsletter: Professor Stefan Krastanov (University of Massachusetts Amherst College of Information and Computer Science) publishes a monthly Julia newsletter. Click here to read it.

MIT News – Julia for Partial Differential Equations: Technique Could Efficiently Solve Partial Differential Equations for Numerous Applications is a new article from MIT News featuring Dr. Chris Rackauckas (JuliaHub VP of Modeling and Simulation) and collaborators about the use of Julia to solve partial differential equations. Click here for more.

Julia for Offshore Wind: Researchers at SINTEF Energy (Norway), including Dr. Avinash Subramanian (JuliaHub Control Engineer) published Hydrogen for Harvesting the Potential of Offshore Wind: A North Sea Case Study, using Julia to analyze the use of hydrogen to harvest offshore wind energy. Click here to read.

Julia for Earth Observation: The second annual Global Workshop on Earth Observation with Julia was held in the Azores January 8-12. João Pinelo, Director of Data Science, Computing and Software Development at the AIR Centre noted that in-person participation was at capacity (44 people) with around 200 total participants, including online. JuliaHub VP of Modeling and Simulation Dr. Chris Rackauckas presented, among others. More information is available from the AIR Centre and from Açores 9.

Julia for Visualizing Molecules: Visualization of Molecules at Surfaces is a new blog post from Alex Riss describing how to use Julia to improve visualizations of molecules. Click here for more.

Digital Echo: A Process-Centric Approach to Industrial Surrogate Modeling

By: Chris Rackauckas

Re-posted from: https://info.juliahub.com/blog/digital-echo-industrial-surrogate-modeling

Industrial surrogate modeling is typically centered around neural network architectures and precision. While important, these approaches don’t necessarily solve the ‘surrogates problem’. The ‘surrogates problem’ refers to the challenges associated with integrating surrogate models into industrial processes and requirements infrastructure. The challenge with this approach is that it doesn’t integrate surrogates into industrial processes and requirements infrastructure.  For surrogate modeling to truly solve real-world industrial engineering problems, verification and validation benchmarks are equally important. This blog post looks at the three key pillars to create surrogates that are process-driven, reliable, and prioritize prediction time for varied industrial applications.