Monday, July 29, 2013

Understanding Electromagnetic 'EM' Waves

Through many strait hours of research I think I am starting to understand the characteristics behind electromagnetic waves - without having taken a class in physics, optics, or electrodynamics! Here I will share my understanding and useful finds . . .

  • There are two common types of waves: Longitudinal and Transverse; Plane was another interesting wave I came across on my voyage. EM waves are a combination of two 2D transverse waves perpendicularly intersecting each other.
  • This is what an electromagnetic wave looks like graphed in 3D space: An E-Field and an M-Field.
    [Figure 1]
  • ...and this is the same graph, except representing elliptical polarization. (If Z were a magnetic field line, a charged particle would whirl around Z over an extended time period.)
    [Figure 2]

  • >> The above graph does not represent a motion or oscillation of a substance but the forces felt by observers. (An EM wave has no electric charge! "Wait, wait,..WHAT?! But what about the 'Electro' in electromagnetism?" There is a difference between an Electric Charge and an Electric Field - don't get them mixed up!)
    A demonstration:

    "Think of the Earth as a giant electron, and you are a proton. Since you are oppositely charged, you are attracted to the Earth, and vice versa. The electric force is similar to gravity in this manner.
    Now think of how your distance from the Earth affects your weight. You know that the farther you are from Earth, the less you weigh, and so the less force you feel. This is similar to the electric field.

    Check out these two equations:
    Weight -> F_weight = mass * gravity
    E-Field -> F_electr = charge * E-field

    A particle's charge is like mass.
    The E-field is like gravity. The stronger gravity, the more force you feel (due to your weight). The stronger the E-field, the more force a charged particle feels." by 
    zee_prime on Yahoo Answers
  • An Electrical Field is the force experienced by a motionless electrically charged particle at any point in space. For example: a Proton has an electrical field of +1, a Neutron: 0, and an Electron: -1 (these values are an ideal state, also commonly and simply known as +, -, and N). The strength of the field is proportional to the number of added charged particles. So E+E+E=-3, P+P+N+E=1, and N+N=0. An E-Field is measured in Newtons-per-Coulomb (N/C) or Volts-per-Meter (V/M).
  • A Magnetic Field is the product of a particle with an electrical field moving through space or spinning. The strength of the field is proportional to the charge of the particle cluster and the velocity of it's travel or speed of it's spin. An M-Field is measured in Teslas or Gauss's (G) for smaller fields (1T = 10,000G). Water can be levitated in an M-Field of approximately 10T!
  • There are two electrical field models, one for each charge: (neutral has no charge or it's charge is 0 so it is discarded here) [Figures 3 & 4]
  • There are two models of fields (electrical and magnetic respectively): [Figures 5 & 6]


    An H-FieldA B-Field
  • The M-Field is crossed with the velocity vector (or in other words is at a right angle to the path the particle is traveling) and the force it exerts is crossed with the M-Field and Velocity. This can simply be seen as an XYZ axes in space where X is the magnetic field, Z is the influencing force, and Y is the  distance/time. As a demonstration:
    - You in a standing position will represent the forward vector - the direction of movement, from your feet to your head.
    - With your left hand, point in front of you. Your left arm represents the direction of the magnetic field.
    - Now, with your right hand, raise it to a T so that you are now pointing it to your right. Your right arm represents the direction of force acting on the particle's surroundings.
Here is a fun fact I learned on the way that boggled my mind. 
It seemed surreal at first but is quite logical now:
  • Electromagnetism is one of the four forces of nature - of course. This force has two parts: an Electrical-Field and a Magnetic-Field.
  • M-Fields do not slow or speed up surrounding particles. WHAT?! Nope! In fact, a magnetic field doesn't do any work at all except change the direction of the particle's travel. When a magnetic field collapses, it is converted into it's respective electrical value until the energy equalizes (aka: potential energy).
  • Permanent Magnets: are they emitting a magnetic field? Yes they are. While there is no distinct current flowing within them, on the quantum level the charged particles are spinning as well as the almost unnoticeable whirl of electrons about their nuclei.
  • Note the B-Field above (Figure 6). If you sever the loop and straighten the path of travel for charged particles, you can get a better view of how a B-Field works. Demonstration:
    [Figure 7]
    As negative electrons move up, a magnetic field emits outward (perpendicular to the electrical field) and rotates counterclockwise looking down (clockwise looking up). Again, an electrically charged particle would spin around a magnetic path just like a magnetic field about an electrical path.

Images from StackExchange and Wikipedia

Thursday, July 25, 2013

Regular Expressions in C# (RegEx)

While working with several code-cleanup regular expressions, I had a thought of putting together a collection of nifty and useful patterns that I could use latter when the time came so I and anyone else wouldn't need to rebuild or re-research them.

The list is small for now, but I will try to add to it in time... If you have useful expression not listed here, do share!



  • {([^{]*?)}
    • This expression matches the objects contents from left curly bracket to the right and returns the body within the brackets $1.
    • If implemented properly in a source code bottom-up decomposer, this expression will be able to help break a string into a tree of objects (TreeNode)
  • \{((?>[^{}]+|\{(?<DEPTH>)|\}(?<-DEPTH>))*(?(DEPTH)(?!)))\} by Tim Pietzcker on StackOverflow
    • This does the same as the above except that it also includes embedded objects.
    • If implemented properly in a source code top-down decomposer, this expression will be able to help break a string into a tree of objects (TreeNode)

  • ^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$
    • This expression matches valid emails.

  • (?<=<body([^<]*?)>)[\s\S]*(?=</body>)
    • Extract HTML body using Look-ahead and Look-behind.
  • (<script([^<]*?)>[\s\S]*?</script>)
    • Select SCRIPT block within body of HTML.

  • [\n]+
    • Remove multiple newline characters: '\n\n\n' => '\n' (replace '\n' with any other character to do the same: [ab]+ => 'aaabbb' => 'ab')

  • "[^\\"]*(?:(?:\\\\)*(?:\\"[^\\"]*)?)*" Hatchi on StackOverflow
    • Capture a string with an embedded string - used for parsing source code. (This one took me some time to find!)
  • \"([^\"]+)\"
    • Capture every other matching string.
      Example: "my"string"has"quotes"!" → { my, has, ! } and not { string, quotes }.

  • (?<=^)
    • Match patterns at the beginning of a string using the 'look-behind' expression combined with the carrot character delimiter.

    I wrote a simple WinForms App to test my expressions on so I would know if they would work the way I intended them to. This editor was also built to apply the expression on the given text, a file, or a tree of files of a given file extension. There is the option to Replace, Capitalize, Lowercase, and Invert-Case.
    The code in this picture is partially parsed java code. Though I am capable of manually parsing Java code to CSharp, it becomes a tedious chore that consomes too much time when there are over 1.2 thousand files to port (and a simple String.Replace will not do the trick - too many variables to consider)!