Code Monkey home page Code Monkey logo

playgroundcalculatorsupport's Introduction

PlaygroundCalculatorSupport

Turn your Swift Playground into an interactive calculator, with units and vector support.

Features

  • Based on Foundation's highly extensible unit system
  • Integrate units into calculations in a quick and concise way
  • Easy-to-use vector support (also with units)
  • Use LANumerics to solve linear equations

Installation

PlaygroundCalculatorSupport is available as a Swift Package. However, currently there's no built-in way to integrate a Swift Package into a Swift Playground inside Xcode. It is recommended to use Arena to do so.

arena -n "PlaygroundCalculator" Cay-Zhang/PlaygroundCalculatorSupport@branch:master

Basic Support

Under Module PlaygroundCalculatorSupport.

Calculation with Units

3.0.cm + 4.m  // 4.03 m
3.cm / 4.cm  // 0.75
3.0 * 4.// 12.0 m³

3.m * 24.degrees  // Error

12.0.cm.converted(to: .meters)  // 0.12 m
30.0.degrees.converted(to: .revolutions)  // 0.08333 rev

// Length -> Area -> Volume
let a = 8.7.cm, b = 7.3.cm
let A_rectangle = a * b  // 0.00635 m²
A_rectangle.converted(to: .squareInches)  // 9.84407 in²
let V_cuboid = A_rectangle * 7.m  // 0.04446 m³

Vectors

// Cartesian Coordinates <x, y>
let vec_a = 3.0.cm ++ 4.0.cm  // <3.0 cm, 4.0 cm>
vec_a.magnitude  // 5.0 cm
vec_a.angle  // 53.1301 °

// Polar Coordinates <r, θ>
let vec_b = sqrt(2).m ++ 135.0.degrees  // <-1.0 m, 1.0 m>

// Dot Product
vec_a * vec_b  // 0.01 m

Linear Algebra (LANumerics)

// Example of solving an electric circuit problem involving a system of linear equations
import PlaygroundCalculatorSupport
import Electricity
import LANumerics

let Va = 15.V, Ra = 18.let Vb = 12.V, Rb = 19.let Vc = 36.V, Rc = 12.let Ia_Ib_Ic = Matrix.withBaseValue([
    [1., 1., 1.],
    [Ra , -Rb, 0.],
    [Ra , 0., -Rc]
]).solve(withBaseValue:
    [0.V, Va - Vb, Va - Vc]
)  // [-0.46183, -0.59542, 1.05725]

let Ia = Ia_Ib_Ic[0].A  // -0.46183 A

Electricity

Under Module Electricity.

Global Constants

  • Vacuum Permittivity 𝝴
  • Coulomb Constant k

Units

Dimension Base Unit
UnitElectricCharge coulombs (C)
UnitElectricResistance ohms (Ω)
UnitElectricPotentialDifference volts (V)
UnitElectricCurrent amperes (A)
UnitCapacitance farads (F)
UnitElectricField newtonsPerCoulomb (NpC)
UnitElectricFlux newtonSquareMetersPerCoulomb

Utilities

Circuits Solving

Example - Resistances

// Find V1
let V = 12.V
let R1 = 13.let R2 = 22.let R3 = 16.let R4 = 18.let R5 = 28.let R6 = 13.let R14 = resistance(series: R1, R4)  // 31.0 Ω
let R124 = resistance(parallel: R14, R2)  // 12.86792 Ω

let V14 = R124 / (R124 + R6) * V  // 5.96937 V
let V1 = R1 / (R1 + R4) * V14  // 2.50328 V

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.