Sally - Simple Algebraic Linear equations solver

Related tags

App sally
Overview

Sally

Sally is a simple solver for Simple Algebraic Linear equations.
Platform-agnostic as a common module in a Kotlin/Multiplatform library.

Why?

Occasionally, even in commercial programming you may run into a problem, which can be represented by a simple equation.
Let's assume, we run a logistic business.
For example, basic business requirement may be to calculate box volume:

boxVolume

fun boxVolume(length: Double, width: Double, height: Double) = length * width * height

The next business requirement may be to calculate length, width or height based on any other 3 known parameters.

fun boxLength(volume: Double, width: Double, height: Double) = volume / width / height
fun boxWidth(volume: Double, length: Double, height: Double) = volume / length / height
fun boxHeight(volume: Double, length: Double, width: Double) = volume / length / width

It is obvious that for the same relationship, which can be represented by a single formula we need 4 methods and tests for each of those.

As an option, we may represent relationship as an expression and delegate methods to that expression.

private fun boxVolume(length: Expr, width: Expr, height: Expr, volume: Expr) = (
    volume - length * width * height
).solve()

fun boxVolume(length: Double, width: Double, height: Double) = boxVolume(
    length = length.asExpr(),
    width = width.asExpr(),
    height = height.asExpr(),
    volume = x
)
fun boxLength(volume: Double, width: Double, height: Double) = boxVolume(
    length = x,
    width = width.asExpr(),
    height = height.asExpr(),
    volume = volume.asExpr()
)
fun boxWidth(volume: Double, length: Double, height: Double) = boxVolume(
    length = length.asExpr(),
    width = x,
    height = height.asExpr(),
    volume = volume.asExpr()
)
fun boxHeight(volume: Double, length: Double, width: Double) = boxVolume(
    length = length.asExpr(),
    width = width.asExpr(),
    height = x,
    volume = volume.asExpr()
)

How-to

Unknown part is marked with an x.

val expr = x - 1
println(expr.solve()) // 1

For explicit conversion of number to expressions use asExpr()

val expr = 10.asExpr() - x * 2.asExpr()
println(expr.solve()) // 5

If there is no x in expression, it will be solved as a simple expression.

val expr = 10.asExpr() - 3 * 2
println(expr.solve()) // 4

More complicated example is in jvmMain/kotlin/main.kt

fun bank(
    fv: Expr,
    pv: Expr, pmt: Expr,
    rate: Double,
    m: Int, n: Int
): Double {
    val i = (rate / 100.00 / m).asExpr()
    val mn = m * n

    return (
        fv - pv * (1 + i).pow(mn) - pmt * ((1 + i).pow(mn) - 1) / i
    ).solve()
}

fun deposit() {
    val fv = bank(
        fv = x,
        pv = 1000.00.asExpr(),
        pmt = 0.asExpr(),
        rate = 10.00,
        m = 12,
        n = 2
    )
    "Deposit FV = %.2f".format(fv).let(::println)
}

fun credit() {
    val pv = 1000.00
    val i = 5.00
    val n = 2

    val fv = bank(
        fv = x,
        pv = pv.asExpr(),
        pmt = 0.asExpr(),
        rate = i,
        m = 1,
        n = n
    )

    val pmt = bank(
        fv = fv.asExpr(),
        pv = 0.asExpr(),
        pmt = x,
        rate = i,
        m = 12,
        n = n
    )

    "Credit PMT = %.2f".format(pmt).let(::println)
}

fun main() {
    deposit()
    credit()
}

Reasoning

Consider common financial formula that represents relationship between Future Value (FV), Present Value (PV), and Periodic Payments (PMT).

fv - pv*(1+i/m)^mn - pmt*((1+i/m)^(mn)-1)/i/m

Where:
is an annual rate.
is a number of years.
is a number of periods of capitalization of interest per year.

For a simple deposit for 2 years with a rate of 10% and an initial amount of 1000 we would need next formula:

For a credit for 2 years with a rate of 10% and an initial amount of 1000 we may use a set of 2 formulas:
This is not an optimal solution, but it allows us to use same relationship.
Firstly, we have to find FV, the value of a loan in 2 years.


Then, we have to find out monthly payments:

As one can observe, same formula is used in all the examples, but in some cases FV, PV or PMT is equal to zero.
However, as a code naive solution would be represented by 3 methods, one for each unknown variable FV, PV or PMT.
This results in an unnecessary growth of codebase, which can be substituted with a simple algebraic solver for linear equations.

Installation

Fork or use Jitpack.
Maven support will be done on request.

You might also like...
This is a simple example of Aspect Oriented Programming in Android
This is a simple example of Aspect Oriented Programming in Android

Android-AOPExample This is a simple example of Aspect Oriented Programming in Android as part of a blog post I have written. The idea was to measure h

A simple app to showcase Androids Material Design and some of the cool new cool stuff in Android Lollipop. RecyclerView, CardView, ActionBarDrawerToggle, DrawerLayout, Animations, Android Compat Design, Toolbar
A simple app to showcase Androids Material Design and some of the cool new cool stuff in Android Lollipop. RecyclerView, CardView, ActionBarDrawerToggle, DrawerLayout, Animations, Android Compat Design, Toolbar

#Android-LollipopShowcase This is a simple showcase to show off Android's all new Material Design and some other cool new stuff which is (new) in Andr

A simple chat demo for socket.io and Android

socket.io-android-chat This is a simple chat demo for socket.io and Android. You can connect to https://socket-io-chat.now.sh using this app. Installa

A simple sample showing the different types of notifications on Andoid

Notification example Simple notification. Expandable notification Progress notification Action button notification Notifications: MainActivity: Refs h

 Nice and simple DSL for Espresso in Kotlin
Nice and simple DSL for Espresso in Kotlin

Kakao Nice and simple DSL for Espresso in Kotlin Introduction At Agoda, we have more than 1000 automated tests to ensure our application's quality and

Simple amazing Donut Progress library🚀
Simple amazing Donut Progress library🚀

DonutProgress 🚀 Now this readme is very poooor 😄 To implement As usual 😒 in project build.gradle: allprojects { repositories { ... maven {

Simple app to Transfer Media and Text from computer to mobile.

TransferByte Simple app to Transfer Media and Text from computer to mobile.Written in kotlin. Recycler view used for listing the media posted and down

Calculator is a simple 4 function calculator

Calculator Calculator is a simple 4 function calculator, with a panel that has more advanced functions when you need them. It includes history, real t

A calculator for quick simple calculations with a nice user interface and no ads
A calculator for quick simple calculations with a nice user interface and no ads

Simple Calculator A calculator with the basic functions and a customizable widget. You can copy the result or formula to clipboard by long pressing it

Owner
Mykyta Sikriier
Mykyta Sikriier
A simple Water Board solver.

WaterSolver A simple Water Board solver. Some variants are very wrong! No need to report this... For now this is it. Might update the mod by being abl

null 4 Nov 18, 2022
Repository contains structures and methods to execute linear algebra operations (matrix multiplication etc)

LinearAlgebra Repository contains structures and methods to execute linear algebra operations (matrix multiplication etc) Matrix class Matrix implemen

null 0 Apr 27, 2022
Simple-todo-app - Simple Memo App using SQLite

Judul Aplikasi Aplikasi Memo Sederhana menggunakan SQLite. Fitur Aplikasi Memo y

Ananda Muhamad Lukman 0 Jan 3, 2022
A simple material design app intro with cool animations and a fluent API.

material-intro A simple material design app intro with cool animations and a fluent API. Very inspired by Google's app intros. Demo: A demo app is ava

Jan Heinrich Reimer 1.7k Jan 7, 2023
A Full-Stack mobile app, including Android & Server, Simple-Poem 简诗. You can write poem in graceful & traditional Chinese style.

JianShi 简诗 A Full-Stack mobile app, including Android side & Server side, Simple-Poem 简诗. You can write poem in graceful & traditional Chinese style.

wingjay 1.9k Jan 6, 2023
Simple Mvp Implementation

Froggy Simple Mvp Implementation Download Gragle: compile 'ru.bullyboo.mvp:froggy:1.0.2' Maven: <dependency> <groupId>ru.bullyboo.mvp</groupId>

Rostislav Sharafutdinov 15 Mar 17, 2019
A simple android Twitter client written in Kotlin

Blum Blum is an unofficial, simple, fast Twitter client written in Kotlin. This project is a complete rewrite of the Java version. Screenshot Build To

Andrea Pivetta 77 Nov 29, 2022
A simple textfield for adding quick notes without ads.

Simple Notes A simple textfield for adding quick notes. Need to take a quick note of something to buy, an address, or a startup idea? Then this is the

Simple Mobile Tools 670 Dec 31, 2022
A simple calendar with events, customizable widgets and no ads.

Simple Calendar A simple calendar with events and a customizable widget. A simple calendar with optional CalDAV synchronization. You can easily create

Simple Mobile Tools 3k Jan 3, 2023
Simple Android movies app using MVVM clean architecture.

Simple Android movies app using MVVM clean architecture.

Marcos Calvo García 57 Dec 30, 2022